if we combine the last two programs to add three numbers and print the result we get this
extern printf
segment .data
patt db "sum=%d",10,0
num1 dd 3
num2 dd 5
num3 dd 7
segment .text
global main
main:
push ebp
mov ebp, esp
mov eax, [num1]
add eax, [num2]
add eax, [num3]
push eax
push patt
call printf
add esp, 8
mov eax, 0
mov esp, ebp
pop ebp
ret
here is an example that uses all four arithmetic operations
; compute and print y = (x - 1) * (x - 5) / (x - 11) where x = 9
; answer should be -16
extern printf
segment .data
resultmsg db "y=%d",10,0
x dd 9
y dd 0
segment .text
global main
main:
push ebp
mov ebp, esp
mov eax, [x] ; eax = x
mov ebx, eax ; ebx = eax = x
mov ecx, eax ; ecx = eax = x (now we have three copies of x)
sub eax, 1 ; eax = eax - 1 = x - 1
sub ebx, 5 ; ebx = ebx - 5 = x - 5
sub ecx, 11 ; ecx = ecx - 11 = x - 11
imul ebx ; edx:eax = eax * ebx
idiv ecx ; eax = edx:eax / ecx (edx gets the remainder)
mov [y], eax
push dword [y]
push resultmsg
call printf
add esp, 8
mov eax, 0
mov esp, ebp
pop ebp
ret