I have this homework in which i have some words from a txt file and, depending on a random number, i must take out one of the words inside, txt file looks like this:
,author0,fear0,dance0,money0,bottle0,pillow0
As you can see there is a ',' before and a '0' after each word. I already have the random number and the import of the file content done and successful, however what i am trying to do to for extract the sub-string and therefore the word, is: looping the whole text content, when it reaches a ',' it compares to know if the counter of ',' appearances is the same as the random number and by then, save the next characters in the buffer variable until finding a '0' so it can return to the start function. I have this code by far:
%include macros.inc ;macros and definition of system calls
%include random.asm ;where the random number is created
section .data
filename: dq "txt/low.txt",10
counter: db 0 ;counts the traveled spaces to know if equals random
random: db 0
section .bss
text resq 18 ;stores all the content in txt file
buffer resb 18;saves the substring looked from text, supposed to be a word
section .text
global _start
_start:
call fileReadingLow ;saves txt file content in 'text' variable
call generateRandomNumber ;saves a random number between 0-9 in 'random'
mov eax,text ;for looping characters in text
mov ecx,48 ;amount of characters in text
call loopWord
print buffer ;macro from macros.inc
exit ;macro
fileReadingLow:
;Open the file
mov rax, SYS_OPEN
mov rdi, filename
mov rsi, O_RDONLY
mov rdx,0
syscall
;Read from file
push rax
mov rdi, rax
mov rax, SYS_READ
mov rsi, text
mov rdx, 128
syscall
;Close file
mov rax, SYS_CLOSE
pop rdi
syscall
ret
loopWord:
push rcx
cmp byte[eax],',' ;if character is a comma
je checkRandom
inc eax
pop rcx
loop checkRandom
checkRandom:
mov ebx,[counter]
cmp ebx,[random]
je getWord ;compare if random equals counter of appearances
inc ebx ;increments counter
mov [counter],ebx
pop rcx ;pop cause it does not reaches this line in loopWord
inc rcx ;same reason as above
jmp loopWord
getWord: ;loop the following characters for adding them to buffer
cmp byte[eax],'0' ;suppossedly last char from the word
je RETURN
mov dl,byte[eax] ;char from text in dl
mov [byte+1],dl ;add the char to buffer
inc eax
jmp getWord
RETURN:
pop rcx
ret
My main problem is that i do not know how to append chars consecutively in my buffer variable, by far it only contains the last character of each word. If someone would know how to do this would solve everything.