Skip to content

Instantly share code, notes, and snippets.

@giuscri
Created March 17, 2015 14:03
Show Gist options
  • Save giuscri/841a07b0d981ca84313b to your computer and use it in GitHub Desktop.
Save giuscri/841a07b0d981ca84313b to your computer and use it in GitHub Desktop.
Nicy stuff ... :)
;; main.asm
segment .data
promptfirstnumber db "Enter the first number: ", 0
promptsecondnumber db "Enter the second number: ", 0
scanfnumber db "%d", 0
printsum db "%d + %d = %d", 10, 0
a dd 0
b dd 0
segment .text
global main
extern printf
extern scanf
extern sum
main:
;; Make room for two local variables ...
enter 4, 0
;; Push ebx on the stack, following C calling conventions ...
push ebx
;; Initialize the two local variables ...
mov dword [ebp-4], a
mov dword [ebp-8], b
;; Ask user to type in the first number ...
push promptfirstnumber
call printf
add esp, 4
;; Scan the first number ...
mov ebx, [ebp-4]
push ebx
mov ebx, scanfnumber
push ebx
call scanf
add esp, 8
;; Ask user to type in the second number ...
push promptsecondnumber
call printf
add esp, 4
;; Scan the second number ...
mov ebx, [ebp-8]
push ebx
mov ebx, scanfnumber
push ebx
call scanf
add esp, 8
;; Push the first number and the second number on the stack ...
mov ebx, [ebp-8]
mov ebx, [ebx]
push ebx
mov ebx, [ebp-4]
mov ebx, [ebx]
push ebx
;; Call sum ...
call sum
;; Restore the stack ...
add esp, 8
;; Print out the result ...
mov ebx, eax
push eax
mov ebx, [ebp-8]
mov ebx, [ebx]
push ebx
mov ebx, [ebp-4]
mov ebx, [ebx]
push ebx
mov ebx, printsum
push ebx
call printf
add esp, 16
;; Restore the value of ebx ...
pop ebx
;; Free room made for the locals ...
leave
;; Return back ...
ret
;; sum.asm
segment .text
global sum
sum:
;; Make room for no local variables ...
enter 0, 0
;; Push ebx on the stack, following C calling conventions ...
push ebx
;; Initialize the value of eax ...
mov eax, 0
;; Add the value of a [ebp+8] to eax ...
add eax, [ebp+8]
;; Add the value of b [ebp+12] to eax ...
add eax, [ebp+12]
;; Restore the value of ebx ...
pop ebx
;; Free no room made for the locals ...
leave
;; Return back ...
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment