1

Please check this code:

extern _printf, _scanf
global _main
section .bss
  num1: resb 4
  num2: resb 4
section .data
  msg1: db "Ingrese un numero: ", 10, 0
  msg2: db "Ingrese otro numero: ", 10, 0
  fmt_i: db "%i", 0
  msg3: db "Los numeros ingresados son: %i y %i", 10, 0
section .text
_main:
  sub esp, 8
  push msg1
  call _printf
  add esp, 12

  sub esp, 12
  push fmt_i
  push num1
  call _scanf
  add esp, 16
ret

The output I get is:

Ingrese un numero: Segmentation fault: 11

I have read Michael Petch(@MichaelPetch)'s great answer about that, but I do not quite understand all that, does anyone have an example code? What's wrong with my code?

Thanks in advance. ^^

Omar Murcia
  • 547
  • 2
  • 11
  • 26
  • 2
    `sub esp, 4` `push num1` `push fmt_i` `call _scanf` `add esp, 12` – Michael Petch Feb 14 '17 at 09:06
  • @MichaelPetch thank you ^^ – Omar Murcia Feb 14 '17 at 11:11
  • 1
    You can verify in debugger your work with `esp` by taking a note of it's initial value upon entering your code, then before the last `ret` the `esp` should be restored to the same value (and the content of stack should be intact at that address, as there's return address stored, so you can validate that too, in case you have stack-overwrite bug in code). – Ped7g Feb 14 '17 at 11:22

0 Answers0