Assembly Language Tutorial (x86)
For more detailed information about the architecture and about processor instructions, you will need access to a 486 (or 386+) microprocessor manual. The one I like is entitled The 80386 book, by Ross P. Nelson. (This book is copyright 1988 by Microsoft Press, ISBN 1-55615-138-1.) Intel processor manuals may also be found at http://www.x86.org/intel.doc/586manuals.htm.The GNU Assembler, gas, uses a different syntax from what you will likely find in any x86 reference manual, and the two-operand instructions have the source and destinations in the opposite order. Here are the types of the gas instructions:
opcode (e.g., pushal) opcode operand (e.g., pushl %edx) opcode source,dest (e.g., movl %edx,%eax) (e.g., addl %edx,%eax)Where there are two operands, the rightmost one is the destination. The leftmost one is the source.
For example,
movl %edx, %eaxmeans
Move the contents of the edx register into the eax register.For another example,
addl %edx,%eaxmeans
Add the contents of the edx and eax registers, and place the sum in theeax register.
Included in the syntactic differences between gas and Intel assemblers is that all register names used as operands must be preceeded by a percent (%) sign, and instruction names usually end in either "l", "w", or "b", indicating the size of the operands: long (32 bits), word (16 bits), or byte (8 bits), respectively. For our purposes, we will usually be using the "l" (long) suffix.
80386+ Register Set
There are different names for the same register depending on what part of the register you want to use. To use the first set of 8 bits of eax (bits 0-7), you would use %al. For the second set of 8 bits (bits 8-15) of eax you would use %ah. To refer to the lowest 16 bits of eax (bits 0-15) together you would use %ax. For the entire 32 bits you would use %eax (90% of the time this is what you will be using). The form of the register name must agree with the size suffix of the instruction.Here are the important processor registers:EAX,EBX,ECX,EDX - "general purpose", more or less interchangeable EBP - used to access data on stack - when this register is used to specify an address, SS is used implicitly ESI,EDI - index registers, relative to DS,ES respectively SS,DS,CS,ES,FS,GS - segment registers - (when Intel went from the 286 to the 386, they figured that providing more segment registers would be more useful to programmers than providing more general- purpose registers... now, they have an essentially RISC processor with only _FOUR_ GPRs!) - these are all only 16 bits in size EIP - program counter (instruction pointer), relative to CS ESP - stack pointer, relative to SS EFLAGS - condition codes, a.k.a. flags
Segmentation
We are using the 32-bit segment addressing feature of the 486. Using 32-bit addressing as opposed to 16-bit addressing gives us many advantages:- No need to worry about 64K segments. Segments can be 4 gigabytes in length under the 32-bit architecture.
- 32-bit segments have a protection mechanism for segments, which you have the option of using.
i486 has 6 16-bit segment registers, listed here in order of importance:
- CS: Code Segment Register
Added to address during instruction fetch. - SS: Stack Segment Register
Added to address during stack access. - DS: Data Segment Register
Added to address when accessing a memory operand that is not on the stack. - ES, FS, GS: Extra Segment Registers
Can be used as extra segment registers; also used in special instructions that span segments (like string copies).
movw seg-reg, seg-regYou can, however, do
movw seg-reg,memory movw memory,seg-reg movw seg-reg,reg movw reg,seg-regNote: If you movw %ss,%ax, then you should xorl %eax,%eax first to clear the high-order 16 bits of %eax, so you can work with long values.
Common/Useful Instructions
mov (especially with segment registers) - e.g.,: movw %es,%ax movl %cs:4,%esp movw _processControlBlock,%cs - note: mov's do NOT set flags pushl, popl - push/pop long pushal, popal - push/pop EAX,EBX,ECX,EDX,ESP,EBP,ESI,EDI call (jumps to piece of code, saves return address on stack) e.g., call _cFunction int - call a software interrupt ret (returns from piece of code entered due to call instruction) iretl (returns from piece of code entered due to hardware or software interrupt) sti, cli - set/clear the interrupt bit to enable/disable interrupts respectively
lea - is Load Effective Address, it's basically a direct pipeline to the address you want to do calculations on without affecting any flags, or the need of pushing and popping flags.
A simple example:
CODE void funtction1() { int A = 10; A += 66; } compiles to... funtction1: 1 pushl %ebp # 2 movl %esp, %ebp #, 3 subl $4, %esp #, 4 movl $10, -4(%ebp) #, A 5 leal -4(%ebp), %eax #, 6 addl $66, (%eax) #, A 7 leave 8 ret
Explanation: 1. push ebp 2. copy stack pointer to ebp 3. make space on stack for local data 4. put value 10 in A (this would be the address A has now) 5. load address of A into EAX (similar to a pointer) 6. add 66 to A ... don't think you need to know the rest
Mixing C and Assembly Language
The way to mix C and assembly language is to use the "asm" directive. To access C-language variables from inside of assembly language, you simply use the C identifier name as a memory operand. These variables cannot be local to a procedure, and also cannot be static inside a procedure. They must be global (but can be static global). The newline characters are necessary.unsigned long a1, r; void junk( void ) { asm( "pushl %eax \n" "pushl %ebx \n" "movl $100,%eax \n" "movl a1,%ebx \n" "int $69 \n" "movl %eax,r \n" "popl %ebx \n" "popl %eax \n" ); }This example does the following:
- Pushes the value stored in %eax and %ebx onto the stack.
- Puts a value of 100 into %eax.
- Copies the value in global variable a1 into %ebx.
- Executes a software interrupt number 69.
- Copies the value in %eax into the global variable r.
- Restores (pops) the contents of the temporary registers %eax and %ebx.
0 comments:
Post a Comment