0

I'm basically trying to convert the ascii values of a string ( -48 if number -55 if letter) and then multiply those values and add them up. I'm okay with the multiplying part but when I try to add them to a "sum" variable I get a seg fault. Here's the code

    section.data 
    factor: dd 7,3,1,7,3,1,7,3,1
    sum: dd 0 
    decNumFormat:
    db      '%d', 10, 0           
    section.text
    main:
    mov     eax, [esp+4]            
    mov     eax, [esp+8]           
    mov ebx, [eax+4]     ; these 3 are to get the input from user       

    mov ecx,dword[factor] ; factor is the value i want to multiply with
    push ebx
    call checksum

    checksum:

    mov al,[ebx] ; to get 1 byte from the input
    add ebx,1
    sub al,55 ; just assume its a letter
    movzx eax,al
    mul ecx
    mov ecx,1 ;change my factor variable
    add [sum], eax ; here's where I'm getting the segmentation fault. I tried mov as well but that also gave me a seg fault.  
    push dword[sum]
    push decNumFormat 
    call printf; I'm just calling print to see if I have a correct value but of course it's not printing because of the seg fault 
    mov al,[ebx]
    add ebx,1
    sub al,48 ;assume its a number
    movzx eax,al
    mul ecx
    mov ecx,1 
    add [sum], eax
    ret 

There are many similar questions I know but I tried everything and I really can't understand the reason for the seg fault. My eax value up until that add is always correct, I already checked it. Its for linux 32-bit and my commands are:

nasm -f elf32 file.asm -o test.o

gcc test.o

./a.out A1 (input string)

  • 1
    Think about what `call` does: after the subroutine returns, execution continues with the instruction following the original `call`. What comes after `call printf` in your program? If nothing, then it is going to execute garbage memory as instructions and probably crash. And likewise, after `call checksum` returns, you will execute the next instruction which is... `checksum` again. – Nate Eldredge Dec 13 '21 at 16:17
  • Unlike in higher-level languages, functions don't automatically return when you stop writing code for them. There's no such construct as a "close brace". You have to explicitly clean up the stack and `ret`. – Nate Eldredge Dec 13 '21 at 16:17
  • @NateEldredge my original code continues, i dont want to return after that point. and regardless of that, I just tried it now with ´ret´ after ´add´ but still seg fault. – hitchikersuniverse Dec 13 '21 at 16:24
  • If this is not your complete code then it's not a [mcve], which is what we need. Also, what OS is this for, and what commands do you use to build and run the program? – Nate Eldredge Dec 13 '21 at 16:31
  • Thanks for adding the build commands. I don't see how `add [sum], eax` can segfault, as the address should be constant, so I might guess that there is a mistake with how you are debugging it and the actual crash is somewhere else. The easiest way to see what is really going on would be to have actual example code that someone can run and debug for themselves. – Nate Eldredge Dec 13 '21 at 17:03
  • yes there is no reason for it to segfault there but up until that point everything works fine, for example i can print the value of eax just above the add comment and it works but once it gets to add it segfaults. anyways, thank you for the help. – hitchikersuniverse Dec 13 '21 at 17:13
  • 3
    You have a typo: it's `section .text`, not `section.text`. Same with `section .data`. This causes your malformed section directives to be interpreted as labels, hence making them ineffective. So everything ends up in the text section which is not writable. Pay attention to the warnings nasm gives you. It should have said something like "label alone on a line without a colon might be in error." – fuz Dec 13 '21 at 17:48
  • @fuz yes that fixed it, thank you for that valuable lesson i was (stupidly) just ignoring the warnings. – hitchikersuniverse Dec 13 '21 at 17:52
  • 1
    @fuz: we actually have a canonical Q&A for that bug since it's come up a few times: [NASM Segmentation fault when modifying a variable that should be in the read-write .data section (section .data doesn't work without a space?)](https://stackoverflow.com/q/60655042) – Peter Cordes Dec 13 '21 at 19:30

0 Answers0