0

I am trying to write a program that accepts 2 digits as user input, and then outputs their sum. I keep getting segmentation error when trying to run program(I am able to input 2 digits, but then the program crashes). I already check answers to similar questions and many of them pointed out to clear the registers, which I did, but I am still getting a segmentation fault.

section .text
    global _main        ;must be declared for linker (ld)
    default rel
_main:                  ;tells linker entry point
   
   call _readData
   call _readData1
   call _addData
   call _displayData

   mov  RAX, 0x02000001 ;system call number (sys_exit)
   syscall

_addData:
    mov byte [sum], 0   ; init sum with 0
    lea EAX, [buffer]   ; load value from buffer to register
    lea EBX, [buffer1]  ; load value from buffer1 to register
    sub byte [EAX], '0' ; transfrom to digit
    sub byte [EBX], '0' ; transform to digit
    
    add [sum], EAX      ; increment value of sum by value from register
    add [sum], EBX      ; increment value of sum by value from 2nd register
    add byte [sum], '0' ; convert to ASCI  
    
    xor EAX, EAX        ; clear registers
    xor EBX, EBX        ; clear registers

    ret


_readData:
    mov RAX, 0x02000003
    mov RDI, 2
    mov RSI, buffer
    mov RDX, SIZE
    syscall
    ret

_readData1:
    mov RAX, 0x02000003
    mov RDI, 2
    mov RSI, buffer1
    mov RDX, SIZE
    syscall
    ret

_displayData:
    mov RAX, 0x02000004
    mov RDI, 1
    mov RSI, sum
    mov RDX, SIZE
    syscall
    ret

section .bss
  SIZE equ 4
  buffer: resb SIZE
  buffer1: resb SIZE
  sum: resb SIZE

I see that, unlike other languages I learned, it is quite difficult to find a good source /tutorial about programming assembly using nasm on x86_64 architecture. Is there any kind of walkthrough for beginners(so I do not need to ask on SO everytime I am stuck :D)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Use a debugger to pinpoint the crash. – Jester Aug 19 '21 at 19:10
  • I tried with `lldb` and it says that `stop reason = EXC_BAD_ACCESS (code=1, address=0x4000)` and points, that: `0x100003f44 <+19>: subb $0x30, (%eax)` – Tomasz Staszkiewicz Aug 19 '21 at 19:11
  • 2
    Yeah, you want to use `rax` as addresses are 64 bits. So `lea rax, [buffer]` and then use `[rax]` (similarly for other addresses). – Jester Aug 19 '21 at 19:12
  • 2
    Note that your code is wrong, if you want to add the two numbers you should do something along the lines of `mov al, [buffer]; add al, byte [buffer1]; sub al, '0'; mov [sum], al`. `add [sum], EAX` is adding the address which is not what you want. – Jester Aug 19 '21 at 19:14
  • I thought that when I `lea eax, [buffer]` I store the value hold in `buffer` and not the memory address itself? – Tomasz Staszkiewicz Aug 19 '21 at 19:15
  • 2
    `lea` is short for "load effective address" so it specifically loads the address and not the value. `mov` loads the value, and then you need to keep in mind that you have bytes so you should use a byte register. As I showed, you can use `mov al, [buffer]`. – Jester Aug 19 '21 at 19:17
  • Thank you @Jester, you are really helpful. Do you have any resources for a newbie like me, where I can learn this kind of stuff? Or should I just keep trying to understand all of these things by myself and just keep asking here? The documentation of nasm is not really helpful, because it uses C libraries really fast :( – Tomasz Staszkiewicz Aug 19 '21 at 19:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236209/discussion-between-tomasz-staszkiewicz-and-jester). – Tomasz Staszkiewicz Aug 19 '21 at 19:26
  • https://stackoverflow.com/tags/x86/info has some helpful links, and looking at compiler output ([How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116)). can be helpful when you know enough asm to be able to understand it (even if you have to look up instructions in Intel's manual, or a PDF scrape of it: https://www.felixcloutier.com/x86/). – Peter Cordes Aug 19 '21 at 21:12
  • Single-stepping with a debugger and watching values in registers change can be really helpful. Would have made it clear right away that you were getting (truncated) addresses, not values. – Peter Cordes Aug 19 '21 at 21:13

0 Answers0