user2138149
user2138149

Reputation: 17170

Misunderstanding of ADD instruction in assembly or program exit value

Tinkering with assembly language again on a 64bit Linux computer, although that shouldn't make a difference.

I will copy my program and talk my way through it. Currently I don't get the answer I would expect. Here we go:

global _start

    section .data
v1 dq 151    ; first variable points to memory location containing "151d"
v2 dq 310    ; uint64_t v2 = 310d
sum dq 0

    section .text
_start:

mov rax, 9       ; rax now contains 9
add [v1], rax    ; v1 now points to a memory location containing 151 + 9 = 160

mov rax, [v2]    ; rax contains the value 310
add rax, 10      ; rax contains the value 310 + 10 = 320
add rax, [v1]    ; rax contains the value 320 + 160 = 480
mov [sum], rax   ; sum now points to a memory location containing the value 480

mov eax, 1       ; system call to "exit"=1
mov ebx, [sum]   ; return value of program is 480
int 0x080        ; call the system interrupt to terminate program

Then to run my program, I do this:

./main.exec; echo $?

The output is:

224

Not 480? I am guessing I have misunderstood how add works, or have misunderstood how to return exit codes to the OS. Am I correct about that?

Upvotes: 1

Views: 104

Answers (1)

cdhowie
cdhowie

Reputation: 169143

The range of guaranteed-to-be-supported exit codes on Linux is 0-255, inclusive. The higher bits of the exit status are reserved in order to convey other information regarding the program's termination. 480 falls outside this range, so the actual exit code is undefined.

However, most implementations will simply truncate the exit code, which is what is happening here: 480 mod 256 = 224.

Upvotes: 2

Related Questions