I essentially want to write a program that takes F and decrements it until its 0, displaying the results like : F E D C B A 9 8 7 6 5 4 3 2 1 0. I specifically have to use a loop to write these out, i can't just say "db 'F E D C B A 9 8 7 6 5 4 3 2 1' , 10, 0". Since I can't use system calls to print (write) hex digits I abandoned that way of thinking. Wherever I go I'm told to use printf since it doesn't require a conversion from number to character. However, printf seems to require a parameter that's usually something along the lines of "db '%x', 10, 0" within a label. That 10 usually being a LF makes it seemingly impossible to write a horizontal list and instead writes a vertical list which I really don't want.
So I tried to take this program:
extern printf
global main
section .data
format_specifier:
db '%x', 10, 0 ;format specifier for printf with LF
section .text
main:
mov rbx, qword 16
loop1: ;loop to decrement and print number in rsi
dec rbx
mov rdi, format_specifier
mov rsi, rbx
xor rax, rax
call printf
cmp rbx, qword 0
jne loop1
mov rax, 60
syscall
and replace the 10 in the format specifier with 0x20 (the space ASCII) to separate the hex digits as they printed. This, of course, resulted in nothing outputting whatsoever.
I have seen that LF flushes the output buffer and I have tried using fflush to do the same with no avail as seen:
extern printf
extern fflush
global main
section .data
format_specifier:
db '%x', 0x20, 0 ;format specifier for printf with LF
section .text
main:
mov rbx, qword 16
loop1: ;loop to decrement and print number in rsi
dec rbx
mov rdi, format_specifier
mov rsi, rbx
xor rax, rax
xor rsi, rsi
call fflush
call printf
cmp rbx, qword 0
jne loop1
mov rax, 60
syscall
I'm unsure whether it is even the right idea to try and use fflush for my program or if I'm just going in the wrong direction. Please help.
You can use
printfwith the space and then a separate line feed. Alsoputcharis a thing. Furthermore you should keep stack 16 byte aligned and using the exit syscall is bad practice.A possible solution:
Caveat: this will print a space after the final
0.Or, you can avoid
printfaltogether: