1 1 Assembly Language:!IA-32 Instructions"2 Goals of this Lecture"• Help you learn how to:"• Manipulate data of various sizes"• Leverage more sophisticated addressing modes "• Use condition codes and jumps to change control flow"• … and thereby …"• Write more efficient assembly-language programs"• Understand the relationship to data types and common programming constructs in high-level languages"• Focus is on the assembly-language code"• Rather than the layout of memory for storing data"• Precepts will cover that, assembler directives, etc."2 3 Variable Sizes in High-Level Language"• C data types vary in size"• Character: 1 byte"• Short, int, and long: varies, depending on the computer"• Float and double: varies, depending on the computer"• Pointers: typically 4 bytes"• Programmer-created types"• Struct: arbitrary size, depending on the fields"• Arrays"• Multiple consecutive elements of some fixed size"• Where each element could be a struct"4 Supporting Different Sizes in IA-32"• Three main data sizes"• Byte (b): 1 byte"• Word (w): 2 bytes "• Long (l): 4 bytes "• Separate assembly-language instructions"• E.g., addb, addw, and addl"• Separate ways to access (parts of) a register"• E.g., %ah or %al, %ax, and %eax"• Larger sizes (e.g., struct)"• Manipulated in smaller byte, word, or long units"3 5 Byte Order in Multi-Byte Entities"• Intel is a little endian architecture"• Least significant byte of multi-byte entity is stored at lowest memory address"• “Little end goes first”"• Some other systems use big endian"• Most significant byte of multi-byte entity is stored at lowest memory address"• “Big end goes first”"00000101 00000000 00000000 00000000 1000 1001 1002 1003 The int 5 at address 1000:"00000000 00000000 00000000 00000101 1000 1001 1002 1003 The int 5 at address 1000:"6 Little Endian Example"Byte 0: ff"Byte 1: 77"Byte 2: 33"Byte 3: 0"int main(void) { int i=0x003377ff, j; unsigned char *p = (unsigned char *) &i; for (j=0; j<4; j++) printf("Byte %d: %x\n", j, p[j]); } Output on a little-endian machine4 7 IA-32 General Purpose Registers"General-purpose registers EAX EBX ECX EDX ESI EDI 31 0 AX BX CX DX 16-bit 32-bit DI SI AL AH BL CL DL BH CH DH 8 7 15 8 cmpb $5, %al jle else incb %al jmp endif else: decb %al endif: C Example: One-Byte Data"char i; … if (i > 5) { i++; else i--; } Global char variable i is in %al, the lower byte of the “A” register.5 9 cmpl $5, %eax jle else incl %eax jmp endif else: decl %eax endif: C Example: Four-Byte Data"int i; … if (i > 5) { i++; else i--; } Global int variable i is in %eax, the full 32 bits of the “A” register. 10 Loading and Storing Data"• Processors have many ways to access data"• Known as “addressing modes”"• Two simple ways seen in previous examples"• Immediate addressing"• Example: movl $0, %ecx"• Data (e.g., number “0”) embedded in the instruction"• Initialize register ECX with zero"• Register addressing"• Example: movl %edx, %ecx"• Choice of register(s) embedded in the instruction"• Copy value in register EDX into register ECX"6 11 Accessing Memory"• Variables are stored in memory"• Global and static local variables in Data or BSS section"• Dynamically allocated variables in the heap"• Function parameters and local variables on the stack"• Need to be able to load from and store to memory"• To manipulate the data directly in memory"• Or copy the data between main memory and registers"• IA-32 has many different addressing modes"• Corresponding to common programming constructs"• E.g., accessing a global variable, dereferencing a pointer, accessing a field in a struct, or indexing an array"12 Direct Addressing"• Load or store from a particular memory location"• Memory address is embedded in the instruction"• Instruction reads from or writes to that address"• IA-32 example: movl 2000, %ecx"• Four-byte variable located at address 2000"• Read four bytes starting at address 2000"• Load the value into the ECX register"• Useful when the address is known in advance"• Global variables in the Data or BSS sections"• Can use a label for (human) readability"• E.g., “i” to allow “movl i, %eax”"7 13 Indirect Addressing"• Load or store from a previously-computed address"• Register with the address is embedded in the instruction"• Instruction reads from or writes to that address"• IA-32 example: movl (%eax), %ecx"• EAX register stores a 32-bit address (e.g., 2000)"• Read long-word variable stored at that address"• Load the value into the ECX register"• Useful when address is not known in advance"• Dynamically allocated data referenced by a pointer"• The “(%eax)” essentially dereferences a pointer"14 Base Pointer Addressing"• Load or store with an offset from a base address"• Register storing the base address "• Fixed offset also embedded in the instruction"• Instruction computes the address and does access"• IA-32 example: movl 8(%eax), %ecx"• EAX register stores a 32-bit base address (e.g., 2000)"• Offset of 8 is added to compute address (e.g., 2008)"• Read long-word variable stored at that address"• Load the value into the ECX register"• Useful when accessing part of a larger variable"• Specific field within a “struct”"• E.g., if “age” starts at the 8th byte of “student” record"8 15 Indexed Addressing"• Load or store with an offset and multiplier"• Base address in a register"• Fixed displacement embedded in the instruction"• Offset computed by multiplying 2nd register with constant "• Instruction computes the address and does access"• IA-32 example: movl 2000(,%eax,4), %ecx"• No base register. Index register EAX (say, value of 10)"• Multiplied by a multiplier of 1, 2, 4, or 8 (say, 4)"• Added to a fixed displacement of 2000 (say, to get 2040)"• Useful to iterate through an array (e.g., a[i])"• Displacement is the start of the array (i.e., “a”); use register if need pointer dereferencing"• Register is the index (i.e., “i”)"• Multiplier is the size of
View Full Document