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]
Brian is a person with amazing ability to absorb technical knowledge/skills and deliver them to other engineers. He has the advantage that he has a real engineering background so he also knows how things function behind the scenes. You can only learn from Brian as he is one of those who studies for the pleasure of learning and sharing. In other words he is an excellent IT trainer
Eric Schlesinger
Vice President, Technology at PROTEUS Technologies