FKayan
FKayan

Reputation: 51

Assembler memory access error

I have a problem with Assembler 64 bit I simply tyring to use the div -> 7/2 but I get an memory acces error not sure why

 section .text

        global _start:
_start:
    xor     rdx,    rdx
    mov     rax,    7
    mov     rbx,    2
    div     rbx,
    mov     rax,    rdx
    ret

I am using the 64 bit registers so there are no problems with the registers so everything should work fine but I get an error don't know why

Edit

I know thats not a problem with assembler but I did something wrong but not sure what

I am using nasm

Update Code

  section .text

    global _start:
_start:
    xor     rdx,    rdx
    mov     rax,    7
    mov     rbx,    2
    div     rbx,
    int 80h
    mov     rax,    1

now it does nothing without any error

Upvotes: 0

Views: 859

Answers (1)

rkhb
rkhb

Reputation: 14409

You can use ret if you use GCC as linker. You have to name the entry-label main. Don't forget to populate EAX with the exitcode.

test.asm:

extern: printf

section .data
    fmt: db `result: %lu  remainder: %lu\n`

section .text

    global main:
main:
    xor     rdx,    rdx
    mov     rax,    7
    mov     rbx,    2
    div     rbx

    mov rdi, fmt
    mov rsi, rax
    ; mov rdx, rdx
    xor eax, eax
    call printf

    mov eax, 0
    ret

Build & run:

nasm -felf64 test.asm
gcc -otest -m64 test.o
./test

If you use LD as linker you have to manage the exit by yourself. Replace the ret block by:

mov ebx,0           ; exit code, 0=normal
mov eax,1           ; exit command
int 0x80            ; interrupt 80 hex, call kernel

or (recommended for x86-64):

mov   rax, 60       ; sys_exit
mov   rdi, 0        ; return 0 (success)
syscall             ; syscall

Build & run:

nasm -felf64 test.asm
ld -e main -m elf_x86_64 -I/lib64/ld-linux-x86-64.so.2 -lc -o test test.o
./test

Upvotes: 2

Related Questions