for(;;){ run next instruction }
#define DATA_PORT 0x378 #define STATUS_PORT 0x379 #define BUSY 0x80 #define CONTROL_PORT 0x37A #define STROBE 0x01 void lpt_putc(int c) { /* wait for printer to consume previous byte */ while((inb(STATUS_PORT) & BUSY) == 0) ; /* put the byte on the parallel lines */ outb(DATA_PORT, c); /* tell the printer to look at the data */ outb(CONTROL_PORT, STROBE); outb(CONTROL_PORT, 0); }
b8 cd ab 16-bit CPU, AX <- 0xabcd b8 34 12 cd ab 32-bit CPU, EAX <- 0xabcd1234 66 b8 cd ab 32-bit CPU, AX <- 0xabcd
+------------------+ <- 0xFFFFFFFF (4GB) | 32-bit | | memory mapped | | devices | | | /\/\/\/\/\/\/\/\/\/\ /\/\/\/\/\/\/\/\/\/\ | | | Unused | | | +------------------+ <- depends on amount of RAM | | | | | Extended Memory | | | | | +------------------+ <- 0x00100000 (1MB) | BIOS ROM | +------------------+ <- 0x000F0000 (960KB) | 16-bit devices, | | expansion ROMs | +------------------+ <- 0x000C0000 (768KB) | VGA Display | +------------------+ <- 0x000A0000 (640KB) | | | Low Memory | | | +------------------+ <- 0x00000000
AT&T syntax | "C"-ish equivalent | |
movl %eax, %edx | edx = eax; | register mode |
movl $0x123, %edx | edx = 0x123; | immediate |
movl 0x123, %edx | edx = *(int32_t*)0x123; | direct |
movl (%ebx), %edx | edx = *(int32_t*)ebx; | indirect |
movl 4(%ebx), %edx | edx = *(int32_t*)(ebx+4); | displaced |
Example instruction | What it does |
pushl %eax |
subl $4, %esp movl %eax, (%esp) |
popl %eax |
movl (%esp), %eax addl $4, %esp |
call 0x12345 |
pushl %eip (*) movl $0x12345, %eip (*) |
ret | popl %eip (*) |
+------------+ | | arg 2 | \ +------------+ >- previous function's stack frame | arg 1 | / +------------+ | | ret %eip | / +============+ | saved %ebp | \ %ebp-> +------------+ | | | | | local | \ | variables, | >- current function's stack frame | etc. | / | | | | | | %esp-> +------------+ /
pushl %ebp movl %esp, %ebpor
enter $0, $0enter usually not used: 4 bytes vs 3 for pushl+movl, not on hardware fast-path anymore
movl %ebp, %esp popl %ebpor
leaveleave used often because it's 1 byte, vs 3 for movl+popl
int main(void) { return f(8)+1; } int f(int x) { return g(x); } int g(int x) { return x+3; }
_main: prologue pushl %ebp movl %esp, %ebp body pushl $8 call _f addl $1, %eax epilogue movl %ebp, %esp popl %ebp ret _f: prologue pushl %ebp movl %esp, %ebp body pushl 8(%esp) call _g epilogue movl %ebp, %esp popl %ebp ret _g: prologue pushl %ebp movl %esp, %ebp save %ebx pushl %ebx body movl 8(%ebp), %ebx addl $3, %ebx movl %ebx, %eax restore %ebx popl %ebx epilogue movl %ebp, %esp popl %ebp ret
_g: movl 4(%esp), %eax addl $3, %eax ret