Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Define main function (for linker)
- global _main
- ; Define externs from C
- extern _printf
- extern _scanf
- extern _exit
- ; Variables
- section .data
- ; 2 integers (filled with 4 bytes of zeroes - 32 bit empty int)
- ; It's just a place in memory, just 32 bits in RAM
- int1: times 4 db 0
- int2: times 4 db 0
- ; Texts, 10 is new line, 0 is null terminator (have to be on string's end)
- format_in: db '%d', 0
- format_out: db 'The result is: %d', 10, 0
- hello: db 'This program will multiply 2 numbers. Awesome, huh?', 10, 10, 0
- message: db 'Type a number: ', 0
- ; Code
- section .text
- _main:
- ; We push hello msg on stack and call
- ; printf(hello)
- push hello
- call _printf
- ; Every argument pushed on stack is 4 bytes long
- ; By adding 4 to stack pointer register we
- ; are clearing the stack.
- add esp, 4
- ; Integer no. 1,
- ; printf(message)
- push message
- call _printf
- add esp, 4
- ; scanf(format_in, int1)
- ; arguments are reversed in asm
- push int1
- push format_in
- call _scanf
- add esp, 8 ; we pushed 2 arguments so 8 bytes
- ; Integer no. 2,
- ; printf(message)
- push message
- call _printf
- add esp, 4
- ; scanf(format_in, int2)
- push int2
- push format_in
- call _scanf
- add esp, 8
- ; Now we have read the input to int1 and int2
- ; We move them to eax and ebx registers to perform multiplication.
- mov eax, [int1]
- mov ebx, [int2]
- ; Mul multiplies eax with specified register
- ; In eax we've got int1 so argument is ebx
- mul ebx
- ; Result is 64-bit in edx (high) and eax (low) registers
- ; We'll display only 32-bit of this number (eax register)
- ; printf(format_out, (int1 * int2))
- push eax
- push format_out
- call _printf
- add esp, 8
- ; Now we are exiting with code 0 (success)
- push 0
- call _exit
- add esp, 4
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement