;;; This program reads an integer limit then counts from 1 to the limt.
;;; Uses plain C I/O and must be linked to the C libraries.
global main
extern printf
extern scanf
section .text
main:
;; Frame pointer
push ebp
mov ebp,esp
;; Push registers. Use them as local value, and don't have
;; to worry about functions messing with them.
push esi
push ebx
;; Issue the prompt.
push prompt ; Pushes the address value promt onto the stack
call printf ; Call printf with argument prompt
add esp,4 ; Removes the argument from the stack.
;; Read the value.
push cnt ; Pushes the address value cnt onto the stack.
push rfmt ; Ditto rfmt
call scanf ; Call scanf with those two arguments.
add esp,8 ; Remove them from the stack.
;; Set up for the loop. Init limit and count in registers.
mov esi, [cnt] ; Move the value at the address cnt into esi
mov ebx,1 ; Move 1 into ebx.
;; Loop test.
ltop: cmp ebx,esi ; Compare ebx and esi
jg finish ; Jump to finish if ebx > esi (ie count > limit)
;; Print count using the printf function.
push ebx ; Push count onto stack (contents of ebx)
push pfmt
call printf
add esp,8
;; Increment the count
inc ebx ; Increment the ebx register.
;; Back to the top
jmp ltop
;; Wrap up and leave.
finish: pop ebx ; Restore these
pop esi
mov esp,ebp ; Fix up frame pointer.
pop ebp
ret ; Go back.
section .data
prompt: db 'Enter count: ',0
rfmt: db '%d',0
pfmt: db ' => %d',10,0
section .bss
cnt: resd 1 ; resd = "Reserve doubleword"