Author Topic: Segmentation fault error using nasm  (Read 14343 times)

Offline Kyi Soe Thin

  • Jr. Member
  • *
  • Posts: 3
Segmentation fault error using nasm
« on: June 10, 2017, 03:57:23 AM »
Hi
I started learning assembly language and I tested jmp instruction with a small program.
Here are my code
Code: [Select]
section .data
        pointer dd one,two,three
        dis1 dd "One"
        dis2 dd "Two"
        dis3 dd "Three"
        lendis1 equ $-dis1
        lendis2 equ $-dis2
        lendis3 equ $-dis3

section .bss
var resd 1

section .text
global main
main:
mov eax,3
mov ebx,0
mov ecx,var
mov edx,1
int 80h
mov edx,var
mov eax,[pointer + edx*4]
jmp [eax]
one:
mov eax,4
mov ebx,1
mov ecx,dis1
mov edx,lendis1
int 80h
jmp exit
two:
mov eax,4
                mov ebx,1
                mov ecx,dis2
                mov edx,lendis2
                int 80h
jmp exit

three:
mov eax,4
                mov ebx,1
                mov ecx,dis3
                mov edx,lendis3
                int 80h
jmp exit

exit:
mov eax,1
int 80h
When I ran this as following
Code: [Select]
root@kalihost:~# nasm -f elf64 jump.asm
root@kalihost:~# gcc -o jump jump.o
root@kalihost:~# ./jump
1
Segmentation fault
root@kalihost:~#
root@kalihost:~#
Please find me the error.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Segmentation fault error using nasm
« Reply #1 on: June 10, 2017, 05:29:38 AM »
I don't know anything about 64-bit code, but I know pointers are 64 bits!

What you've got there looks like pretty good 32-bit code. Mixing 64- and 32-bit code may not be a good idea. You might want to assemble it as 32-bit code, and tell gcc "-m32".

In any case, you're calculating the lengths wrong. Put the equs immediately after what you want the lengths of.

You need to convert stdin from characters to a number. You're using the address of "var" in your effective address.

My advice, if you're just beginning, would be to start with something simpler and take smaller steps.

Best,
Frank



Offline Kyi Soe Thin

  • Jr. Member
  • *
  • Posts: 3
Re: Segmentation fault error using nasm
« Reply #2 on: June 10, 2017, 03:12:23 PM »
I want to ask one more question.
Can jmp instruction be used with variable ?
For example,
Code: [Select]
section .data
        pointer dw "one","two","three"
        dis1 dd "one"
        lendis1 equ $-dis1
dis2 dd "Two"
        lendis2 equ $-dis2
dis3 dd "Three"
        lendis3 equ $-dis3

section .bss
var resd 1

section .text
global main
main:
mov eax,3
mov ebx,0
mov ecx,var
mov edx,1
int 80h
mov eax,pointer
jmp eax
one:
mov eax,4
mov ebx,1
mov ecx,dis1
mov edx,lendis1
int 80h
jmp exit
two:

mov ecx,dis2
mov eax,4
                mov ebx,1
                mov edx,lendis2
                int 80h
jmp exit

three:
mov eax,4
                mov ebx,1
                mov ecx,dis3
                mov edx,lendis3
                int 80h
jmp exit

exit:
mov eax,1
int 80h
Please help me.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Segmentation fault error using nasm
« Reply #3 on: June 11, 2017, 04:13:43 AM »
Quote
I want to ask one more question.
Can jmp instruction be used with variable ?

Yes.

... not with random garbage in the variable, though.

Since this is mostly 32-bit code (64-bit uses "syscall" not "int 80h"), I tried it as 32 bit.

Code: [Select]
; nasm -f elf32 myprog.asm
; ld -m elf_i386 -o myprog myprog.o

section .data
        pointer dd one,two,three

        dis1 db "one"
        lendis1 equ $-dis1
dis2 db "Two"
        lendis2 equ $-dis2
dis3 db "Three"
        lendis3 equ $-dis3

section .bss
var resd 1

section .text
global _start
_start:
mov eax,3
mov ebx,0
mov ecx,var
mov edx,1
int 80h

mov edi, [var]
sub edi, '0' ; convert character to number
dec edi ; pointer + 0 is "one"

mov eax,[pointer + edi * 4]
jmp eax
; or...
jmp [pointer + edi * 4]


one:
mov eax,4
mov ebx,1
mov ecx,dis1
mov edx,lendis1
int 80h
jmp exit
two:

mov ecx,dis2
mov eax,4
                mov ebx,1
                mov edx,lendis2
                int 80h
jmp exit

three:
mov eax,4
                mov ebx,1
                mov ecx,dis3
                mov edx,lendis3
                int 80h
jmp exit

exit:
mov eax,1
int 80h

Works for me. It may not be what you have in mind...

Best,
Frank


Offline Kyi Soe Thin

  • Jr. Member
  • *
  • Posts: 3
Re: Segmentation fault error using nasm
« Reply #4 on: June 12, 2017, 01:49:19 PM »
Yesss, I get it.
It really works for me !!!
I really really thank you, Frank Kotler.