Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

assembly - 32 bit registers act as 8 bit ones

I've been having the strangest problem. In x86 assembly, the 32 bit registers (eax, ebx, etc.) have been overflowing at 256, suggesting that they're actually 8 bit, for some reason. For example:

test.s:

section .data
section .text

global _start
_start:
    mov eax, 1
    mov ebx, 256
    int 80h

If I then compile this code with nasm -felf32 -g test.s && ld -m elf_i386 -s -o test test.s, and run the resulting executable, it returns 0. This same problem happens for eax, ecx, edx, etc.

Why would the 32 bit registers act like 8 bit ones, in ANY situation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's not the register wrapping around, it's the exit system call, which only uses the lower eight bits of ebx for the return code.

From the exit man-page:

The exit() function causes normal process termination and the value of status & 0377 is returned to the parent (see wait(2)).

That 0377 is the octal equivalent of 0xff (binary 1111 1111), meaning that only the lower eight bits are used. The other bits in what you get back from wait() (in the parent) are used for things such as whether the child process was terminated, what signal was used if so, whether a core dump occurred, and so on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...