NetRun & Real Assembly
CS 301 Lecture, Dr. Lawlor, 2005/09/21
Grab an account from:
https://lawlor.cs.uaf.edu/netrun/pwreset
This will email you your account password (to a UAF email address).
(NOTE: I'm hacking on NetRun pretty extensively, so be sure to let me
know if you find any bugs or have suggestions for making it work
better.)
Log in, and try it out now:
mov $4,%eax # Comment
This is GNU assembly code for x86, which you'll have to select from
the dropdowns. You can also just click on the assembly.
Ok, so this returns 4. Why--what the heck does this mean? Let's look at it piece by piece:
- mov makes sense--it's the assembler "mnemonic" (memorable human-readable string) for the x86 move opcode.
- $4 is the thing we're moving--the constant. The dollar sign
indicates (in gcc assembly!) that this is a constant--an "immediate"
value. Warning: if you leave off the dollar sign, a bare "4" is
interpreted as a (hardcoded!) memory address, which in most cases
results in an immediate segfault! On the plus side, you can write
constants in decimal (normally), octal (by starting with a zero), or
hex (by starting with 0x) exactly like C.
- %eax is the register we're moving *to*. Note that IN GNU
ASSEMBLY, the assignment target is at the end (other assemblers use a
different convention). The value in %eax is the return value of the function.
- # sign begins a comment, just like a UNIX shell script.
How is this different from C? Well, like most assemblers,
- It's line-oriented. Try putting a newline after the
comma--it doesn't compile (er, assemble). Each line has one
instruction.
- It's not case sensitive. "MoV" and "eAx" work just as well as above.
All the assembler does is take this line, and spit out the
corresponding machine code. NetRun disassembles the resulting
code as:
Disassembly of section .text:
00000000 <foo>:
0: b8 04 00 00 00 mov $0x4,%eax
5: c3 ret
The x86 "mov 32-bit immediate value into register eax" instruction
opcode is 0xB8. It's followed by the 32-bit (4-byte) value to
move, stored in the little-endian byte order that is standard on x86, so the "04" comes first followed by the higher-value bytes which are zeros.
Next class, we'll see quite a few other x86 registers & instructions, to do useful stuff in assembly.