Hello all!
I am trying to complete a lab assignment for my computer systems class and we have to use the printf function to print changing register values (increment eax from 1 to 10, decrement ebx from 10 to 1).
Here is my code:
; Purpose: To print data to screen using printf
; Assemble: nasm -f elf -l printflab.lst printflab.asm
; Link: gcc -o printflab printflab.o
extern printf ; The C function to be called
section .data ; data section
fmt: db "eax=%d, ebx=%d", 10, 0 ; The printf format, "\n", '0'
section .text ; code section
global main ; standard gcc entry point
main: ; program label for entry point
mov ecx, 10 ; set register ecx to value 10
mov eax, 1 ; set register eax to value 1
mov ebx, 10 ; set register ebx to value 10
back1: ; loop label
inc eax ; increment eax register
dec ebx ; decrement ebx register
push dword fmt ; address of ctrl string
call printf ; Call C function
loop back1 ; loop until ecx = 0
endprog: ; exit the program
mov esp, ebp ; takedown stack frame
pop ebp ; same as "leave" op
mov ebx, 0 ; 0 = normal
mov eax, 1 ; 1 = no error
int 0x80 ; interrupt 80 hex, call kernel
After assembly and linking, the program just loops infinitely with the same number in both the eax and ebx registers. Am I missing something (like pushing the eax and ebx registers into the stack)?
Thanks in advance for your help