SpaceNugget
SpaceNugget

Reputation: 129

error: invalid operands in non-64-bit mode

I'm trying to run this code and print the result, but for some reason I get this error message:

main.asm:10: error: invalid operands in non-64-bit mode 
main.asm:11: error: invalid operands in non-64-bit mode
main.asm:12: error: invalid operands in non-64-bit mode
main.asm:13: error: invalid operands in non-64-bit mode
ld: cannot find *.o: No such file or directory

This is the code:

global _start       
section .data
    n DB 10
section .text
_start:
    xor ax, ax
    mov bx, 1
    mov cx, (n)
.L1:
    mov r9w, bx    #one of the lines that leads to an error
    imul r9w, bx   #one of the lines that leads to an error
    imul r9w, bx   #one of the lines that leads to an error
    add ax, r9w    #one of the lines that leads to an error
    inc bx
    dec cx
    test cx, cx
    jne .L1
    
    movq rax, 1
    movq rdi, 1
    movq rsi, ax
    movq rdx, 8
    syscall

    xor rax, rax
    ret
END:

I am fairly new to assembly and can't quite understand what the problem is - the BX register is 16 bits, and R9W is also 16 bits...
I use an online compiler to run this (https://www.tutorialspoint.com/compile_assembly_online.php)

Upvotes: 1

Views: 278

Answers (1)

Markian
Markian

Reputation: 342

I see three main problems with your code:

  1. You are attempting to access a 64-bit register in a non 64-bit mode. Despite R9W being the lowest part of the R9 register it is still 64-bit mode only.
  2. You are attempting to return from _start by executing the ret instruction. This can cause a segmentation fault because the stack currently contains a pointer to argc
  3. You should probably use the mov instruction rather than the movq instruction.

Upvotes: 3

Related Questions