Advertisement
Guest User

Untitled

a guest
May 4th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. ; Define main function (for linker)
  2. global _main
  3.  
  4. ; Define externs from C
  5. extern _printf
  6. extern _scanf
  7. extern _exit
  8.  
  9. ; Variables
  10. section .data
  11. ; 2 integers (filled with 4 bytes of zeroes - 32 bit empty int)
  12. ; It's just a place in memory, just 32 bits in RAM
  13. int1: times 4 db 0
  14. int2: times 4 db 0
  15. ; Texts, 10 is new line, 0 is null terminator (have to be on string's end)
  16. format_in: db '%d', 0
  17. format_out: db 'The result is: %d', 10, 0
  18. hello: db 'This program will multiply 2 numbers. Awesome, huh?', 10, 10, 0
  19. message: db 'Type a number: ', 0
  20.  
  21. ; Code
  22. section .text
  23. _main:
  24. ; We push hello msg on stack and call
  25. ; printf(hello)
  26. push hello
  27. call _printf
  28. ; Every argument pushed on stack is 4 bytes long
  29. ; By adding 4 to stack pointer register we
  30. ; are clearing the stack.
  31. add esp, 4
  32.  
  33. ; Integer no. 1,
  34. ; printf(message)
  35. push message
  36. call _printf
  37. add esp, 4
  38.  
  39. ; scanf(format_in, int1)
  40. ; arguments are reversed in asm
  41. push int1
  42. push format_in
  43. call _scanf
  44. add esp, 8 ; we pushed 2 arguments so 8 bytes
  45.  
  46. ; Integer no. 2,
  47. ; printf(message)
  48. push message
  49. call _printf
  50. add esp, 4
  51.  
  52. ; scanf(format_in, int2)
  53. push int2
  54. push format_in
  55. call _scanf
  56. add esp, 8
  57.  
  58. ; Now we have read the input to int1 and int2
  59. ; We move them to eax and ebx registers to perform multiplication.
  60. mov eax, [int1]
  61. mov ebx, [int2]
  62.  
  63. ; Mul multiplies eax with specified register
  64. ; In eax we've got int1 so argument is ebx
  65. mul ebx
  66. ; Result is 64-bit in edx (high) and eax (low) registers
  67. ; We'll display only 32-bit of this number (eax register)
  68.  
  69. ; printf(format_out, (int1 * int2))
  70. push eax
  71. push format_out
  72. call _printf
  73. add esp, 8
  74.  
  75. ; Now we are exiting with code 0 (success)
  76. push 0
  77. call _exit
  78. add esp, 4
  79. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement