note a great book for learning ia32 assembly is Professional Assembly Language by Richard Blum published by Wrox
int 0x80You pass the parameters to the system called with the registers
Example:
movl %4, %eax ; write system call = 4 movl %1, %ebx ; write to STDOUT (file discriptor 1) movl $output, %ecx ; write the value that is located ; at the memory address of output label movl %12, %edx ; write 12 bytes int 0x80 ; do the system call
Stack | |
---|---|
... | Strings of Environmental variables and Command line arguments |
ESP - 4(n+2+m+1) | 0x00000000 (delimiter) |
ESP - 4(n+2+m) | pointer to environmental variable m |
... | pointer to environmental variable . |
... | pointer to environmental variable . |
ESP - 4(n+4) | pointer to environmental variable 2 |
ESP - 4(n+3) | pointer to environmental variable 1 |
ESP - 4(n+2) | 0x00000000 (delimiter) |
ESP - 4(n+1) | pointer to cmd arg n |
... | pointer to cmd arg . |
... | pointer to cmd arg . |
ESP -12 | pointer to cmd arg 2 |
ESP -8 | pointer to cmd arg 1 |
ESP -4 | pointer to program name |
ESP | # of arguments |
Once your done, you should add back to the stack to offset the values pushed onto it before the function call. example:
c function:
int myfunction(int x,int y)
assembly code to call it:
pushl y pushl x call myfunction addl $8, %esp ; add 8 to the stack to "remove" y and x
For Example
pushl %ebp movl %esp, %ebp ; now assume we want 3 32 bit local variables subl $12, %esp ; move stack beneath your local variables ; remember when pushing to the stack the ; stack will decrease by the number of required bytes ; BEFORE writing the data! so you only have to -12 ; rather than -16 ; now var1 = -4(%ebp) ; now var2 = -8(%ebp) ; now var1 = -12(%ebp) ... do stuff ... movl %ebp, %esp ; restore the ESP popl %esp ; restore the EBP
ATT&T operand ordering
opcode src dest
Push and Pop ordering
Push: first decrements ESP, then writes the data to the new address of ESP
so ESP-, [ESP] <= data NOT [ESP] <= data; ESP-
Pop: first reads the data at ESP, then adds to ESP
so data<=[ESP]; ESP+ NOT [ESP]+; data<=[ESP]
Information is power. With Paladin Group, LLC on your side the knowledge and experience of our experts puts the power in YOUR hands!