0

I want to display the least significant digit with an assembly program on nasm/macho-64:

%define SYSCALL_WRITE 0x2000004
%define SYSCALL_EXIT  0x2000001

SECTION .data
   digit db 0,10 

SECTION .text
   global _start

_start:

   add rax, 48
   mov [digit], al

   mov rax, SYSCALL_WRITE
   mov rdi, 1
   mov rsi, digit
   mov rdx, 2  ;length (of 48)
   syscall

   mov rax, SYSCALL_EXIT
   mov rdi, 0
   syscall

Here, al is used because it contains the lowest order byte of rax. I figured out from similar questions that 64-bit assembly doesn't have the [] syntax. I then tried to use

lea digit, al

and

lea [digit], al

but both gave me "invalid combination of opcode and operation" error. I also tried mov rax instead of add rax. What should I do in order to load/move the least significant digit of rax into my digit variable?

Thank you.

srsrso
  • 111
  • 8
  • 1
    Add `default rel` to the top of the file or use `[rel digit]`. PS: your code will not give you the least significant digit unless your number is single digit (i.e. 0-9). Also, what do you expect to find in `rax` at process start? Maybe you forgot to initialize it? – Jester Dec 19 '17 at 12:07
  • `lea` will only calculate address and store it into register, it will never contact memory chip, so it definitely can't be used to read or write value from/to memory. I.e. `mov rax,[rax+rdx*8]` loads value from memory, and `lea` with same operands `lea rax,[rax+rdx*8]` will only calculate the address of memory, and put that into `rax`, instead of loading value from there. It's like first stage of `mov`, before contacting memory chip. ... throwing out random instructions will not help in assembly at all, keep re-reading the instruction descriptions and experimenting in debugger, till it's clear – Ped7g Dec 19 '17 at 12:14
  • @Jester Thanks! it worked. And yeah, this isn't the whole code – it's just a subroutine. I'll initialise `rax` in my `main` routine and then call `_start` – srsrso Dec 19 '17 at 12:14
  • 1
    Don't use the name `_start` for anything other than your actual entry point. In MacOS X, you can only write `_start` yourself in a statically-linked executable; the dynamic linker runs the CRT startup code and calls your entry point as `main`. (Unlike on Linux). – Peter Cordes Dec 19 '17 at 14:32

0 Answers0