I am using the fgets() function call in my arm assembler program and looping through the contents of a file and printing it. All of this works, but I want to exit the program when fgets() returns NULL. I can't actually figure out what I'm supposed to be comparing it too. Here is what I have:
read_file:
mov r2, r5 //file pointer is stored in R5
ldr r0, =buffer
mov r1, #254
bl fgets
bl printf
cmp r0,#0
bne read_file
bl exit
I'm assuming I should be checking the buffer which is getting the string, but I might need to be checking another register for a NULL return?
CodePudding user response:
The problem was that I was comparing the wrong return value. I had worked out correctly that the return value should be stored in R0, but what I am actually comparing here with this code is the return value of the printf() not the return value of fgets() which has already been overwritten at the point I am doing a comparison.
bl fgets
bl printf
cmp r0,#0
After moving the comparison higher up in the code and directly after the fgets() it works.
read_file:
mov r2, r5
ldr r0, =buffer
mov r1, #254
bl fgets
cmp r0, #0
beq exit
bl printf
bl read_file
bl exit