Author Topic: Segmentation Fault Problem In My Simple NASM Code... [Solved]  (Read 19940 times)

Offline monsterhunter445

  • Jr. Member
  • *
  • Posts: 8
Segmentation Fault Problem In My Simple NASM Code... [Solved]
« on: February 13, 2011, 05:00:53 AM »
Hi everyone, I am having a slight problem in making this program that adds two operands. I am writing this NASM code in Ubuntu 10, and I seem to be getting a segmentation fault. I know what a segmentation fault is but with the lack of experience in NASM, and the lack of debugging. I just can pinpoint the cause the of my problem. I would appreciate anybodys  help in solving this simple problem. Here is the code below:

Code: [Select]
section .data
msg1: db 'Please Enter A Number: ', 10
length1: equ $ - msg1
msg2: db 'Please Enter A Second Number: ', 10
length2: equ $ - msg2
zero: db '0', 0
final: db 'Now adding the two operands...', 10
length3: equ $ - final
msg3: db 'The answer is: ', 0
length4: equ $ - msg3
section .bss
operand1: resb 255
operand2: resb 255
io: resb 255
io2: resb 255
answer: resb 255
sanswer: resb 255
section .text
global _start

_start:

; Print first message

mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, length1
int 80h

; Now read value

mov eax, 3
mov ebx, 0
mov ecx, operand1
mov edx, 255
int 80h

; Print second message

mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, length2
int 80h

; Now read second value

mov eax, 3
mov ebx, 0
mov ecx, operand2
mov edx, 255
int 80h

; Convert operand1 to integer

mov ebx, operand1
sub ebx, 48
mov [io], ebx

; Convert operand2 to integer

mov ebx, operand2
sub ebx, 48
mov [io2], ebx

; Printing final message before giving the answer.
mov eax, 4
mov ebx, 1
mov ecx, final
mov edx, length3
int 80h

; Add operand1 and 2

mov eax, io
add eax, io2
mov [answer], eax


; Convert answer to string
mov ebx, answer
mov eax, 10
div ebx
add ebx, zero
mov [sanswer], ebx

; Print final message stating answer.

mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, length4

mov eax, 4
mov ebx, 1
mov ecx, sanwer
mov edx, 510
int 80h

; Exiting

mov eax, 1
mov ebx, 0 ; Returns an exit code of zero which means success.
   
   
   
« Last Edit: February 13, 2011, 08:04:24 PM by monsterhunter445 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Segmentation Fault Problem In My Simple NASM Code...
« Reply #1 on: February 13, 2011, 07:59:04 AM »
You've got a number of problems here, mostly dealing with the difference between an address in memory and the "[contents]" of that address. In several instances, you're operating on the address, not the numbers you've entered....

Code: [Select]
section .data
msg1: db 'Please Enter A Number: ', 10
length1: equ $ - msg1
msg2: db 'Please Enter A Second Number: ', 10
length2: equ $ - msg2
zero: db '0', 0
final: db 'Now adding the two operands...', 10
length3: equ $ - final
msg3: db 'The answer is: ', 0
length4: equ $ - msg3
section .bss
operand1: resb 255
operand2: resb 255
io: resb 255
io2: resb 255
answer: resb 255
sanswer: resb 255
section .text
global _start

_start:

; Print first message

mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, length1
int 80h

; Now read value

mov eax, 3
mov ebx, 0
mov ecx, operand1
mov edx, 255
int 80h

; Print second message

mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, length2
int 80h

; Now read second value

mov eax, 3
mov ebx, 0
mov ecx, operand2
mov edx, 255
int 80h

; Convert operand1 to integer

mov ebx, operand1

; you're loading the address of operand1
; you probably want [operand1]

sub ebx, 48
mov [io], ebx

; Convert operand2 to integer

mov ebx, operand2

; and here

sub ebx, 48
mov [io2], ebx

; Printing final message before giving the answer.
mov eax, 4
mov ebx, 1
mov ecx, final
mov edx, length3
int 80h

; Add operand1 and 2

mov eax, io

; and here

add eax, io2

; and here

mov [answer], eax


; Convert answer to string
mov ebx, answer
        mov eax, 10
div ebx
add ebx, zero
mov [sanswer], ebx
; this is completely wrong.

; Print final message stating answer.

mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, length4
; int 80h?

mov eax, 4
mov ebx, 1
mov ecx, sanswer
mov edx, 510
; 510???
int 80h

; Exiting

mov eax, 1
mov ebx, 0 ; Returns an exit code of zero which means success.
; int 80h?

You also leave out an "int 80h" in a couple of places. Now... the "div" instruction divides the value in edx:eax by the operand you provide (ebx, in this case). The quotient appears in eax, and the remainder in edx - that's the digit you want to add '0' to. Something more like:

Code: [Select]
mov eax, [answer]
mov edx, 10

xor edx, edx ; or mov edx, 0
div ebx
add dl, '0' ; or add dl, [zero]
mov [sanswer], dl
...

That'll only do a single digit. Both the string-to-number routines and the number-to-string routine will have to be modified to do multiple digits, but that should get you started. I suspect the actual segfault is caused by the lack of the final "int 80h"...

Best,
Frank


Offline monsterhunter445

  • Jr. Member
  • *
  • Posts: 8
Re: Segmentation Fault Problem In My Simple NASM Code...
« Reply #2 on: February 13, 2011, 03:02:50 PM »
Hey thanks for pointing out my mistakes, now I don't get an error anymore. The problem is that I always get zero as my answer. So I suspect it is the division that is giving me the problem. Here is the corrected code below:

Code: [Select]
; Add operand1 and 2

mov eax, io
add eax, io2
mov [answer], eax


; Convert answer to string

mov edx, 10
xor edx, edx
div eax
add dl, [zero]
mov [sanswer], dl

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Segmentation Fault Problem In My Simple NASM Code...
« Reply #3 on: February 13, 2011, 03:38:42 PM »
You're still adding the address(es) of io1 and io2...

Best,
Frank


Offline monsterhunter445

  • Jr. Member
  • *
  • Posts: 8
Re: Segmentation Fault Problem In My Simple NASM Code...
« Reply #4 on: February 13, 2011, 08:02:33 PM »
Thanks for your help, It works like a charm now. How do I mark you as answer!