0

I wrote this simple assembly code (nasm on linux) and expected to have rbx storing the address of the memory space that saved a multiplication of floats:

section .data
real1:  dq 25.665544
real2:  dq 10.000

section .text
global _start
_start:
fld qword [real1]       ; load real1 in the float stack, st0
fld qword [real2]       ; st0 now holds real2, real1 in st1
fmul st0, st1           ; st0 *= st1
fstp qword [real1]      ; save st0 in real1 and pop, real1 has the result
fstp qword [real2]      ; save st1 in real2 and pop, float stack is empty
mov rbx, qword real1    ; store the address of real1 in rbx

mov rax, 1
int 80h

After compiling the program and running, I do an "echo $?" to see its return value. Well, it shows 224 on my terminal, but I was expecting to see something like the memory address, which I believe is saved in rbx at the end. Or maybe my expectation is wrong. Can you clarify what is happening?

JayY
  • 109
  • 10
  • 1
    see also: [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](//stackoverflow.com/q/46087730). Also no reason to use x87 for `double` in 64-bit mode; SSE2 is baseline. And IDK why you'd want to return the address of `real1`. – Peter Cordes Oct 11 '19 at 22:47
  • @PeterCordes Thanks for your suggestions, and now that I read the link, I agree it is duplicate. There is no particular reason to return an address except for my process of learning by trying and seeing how things work. – JayY Oct 12 '19 at 22:34
  • 1
    Ok that's fine, as long as you know you're getting an address and leaving the result of your computation unused. Use a debugger to single-step through asm and watch register values change; it shows you a lot more than adding debug-prints or exit status outputs. See the bottom of https://stackoverflow.com/tags/x86/info for asm debugging tips with GDB and `strace`. – Peter Cordes Oct 12 '19 at 22:38

1 Answers1

1

Linux exit codes are only 8 bits long, so the number you're returning is being truncated from 64 bits to 8.

  • 1
    Technically the truncation only happens in the `wait` / `waitpid` API. A `SIGCHLD` handler can get the full 4 bytes: [ExitCodes bigger than 255, possible?](https://stackoverflow.com/q/179565) (Not 8 bytes: `exit(int)`, plus in this particular case the `int 0x80` ABI itself truncates to 32-bit before `sys_exit` even gets its hands on the value.) – Peter Cordes Mar 11 '23 at 07:47