Assembly Language: IA-32 InstructionsGoals of Today’s LectureVariable Sizes in High-Level LanguageSupporting Different Sizes in IA-32Byte Order in Multi-Byte EntitiesLittle Endian ExampleIA-32 General Purpose RegistersC Example: One-Byte DataC Example: Four-Byte DataLoading and Storing DataAccessing MemoryDirect AddressingIndirect AddressingBase Pointer AddressingIndexed AddressingIndexed Addressing ExampleEffective Address: More GenerallyData Access Methods: SummaryControl FlowCondition CodesCondition Codes (continued)Example Five-Bit ComparisonsJumps after Comparison (cmpl)Branch InstructionsJumpingJumping (continued)Slide 27Arithmetic InstructionsBitwise Logic InstructionsData Transfer InstructionsConclusions1Assembly Language:IA-32 InstructionsProfessor Jennifer Rexfordhttp://www.cs.princeton.edu/~jrex2Goals of Today’s Lecture•Help you learn…To manipulate data of various sizesTo leverage more sophisticated addressing modes To use condition codes and jumps to change control flow•Focusing on the assembly-language codeRather than the layout of memory for storing data•Why?Know the features of the IA-32 architectureWrite more efficient assembly-language programsUnderstand the relationship to data types and common programming constructs in higher-level languages3Variable Sizes in High-Level Language•C data types vary in sizeCharacter: 1 byteShort, int, and long: varies, depending on the computerFloat and double: varies, depending on the computerPointers: typically 4 bytes•Programmer-created typesStruct: arbitrary size, depending on the fields•ArraysMultiple consecutive elements of some fixed sizeWhere each element could be a struct4Supporting Different Sizes in IA-32•Three main data sizesByte (b): 1 byteWord (w): 2 bytes Long (l): 4 bytes •Separate assembly-language instructionsE.g., addb, addw, and addl•Separate ways to access (parts of) a registerE.g., %ah or %al, %ax, and %eax•Larger sizes (e.g., struct)Manipulated in smaller byte, word, or long units5Byte Order in Multi-Byte Entities•Intel is a little endian architectureLeast significant byte of multi-byte entity is stored at lowest memory address“Little end goes first”•Some other systems use big endianMost significant byte of multi-byte entity is stored at lowest memory address“Big end goes first”000001010000000000000000000000001000100110021003The int 5 at address 1000:000000000000000000000000000001011000100110021003The int 5 at address 1000:6Little Endian ExampleByte 0: ffByte 1: 77Byte 2: 33Byte 3: 0int 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 machine7IA-32 General Purpose RegistersGeneral-purpose registersEAXEBXECXEDXESIEDI310AXBXCXDX16-bit 32-bitDISIALAHBLCLDLBHCHDH8 7158cmpb $5, %aljle elseincb %aljmp endifelse:decb %alendif:C Example: One-Byte Datachar i;…if (i > 5) { i++;else i--;}Global char variable i is in %al, the lower byte of the “A” register.9cmpl $5, %eaxjle elseincl %eaxjmp endifelse:decl %eaxendif:C Example: Four-Byte Dataint i;…if (i > 5) { i++;else i--;}Global int variable i is in %eax, the full 32 bits of the “A” register.10Loading and Storing Data•Processors have many ways to access dataKnown as “addressing modes”Two simple ways seen in previous examples•Immediate addressingExample: movl $0, %ecxData (e.g., number “0”) embedded in the instructionInitialize register ECX with zero•Register addressingExample: movl %edx, %ecxChoice of register(s) embedded in the instructionCopy value in register EDX into register ECX11Accessing Memory•Variables are stored in memoryGlobal and static local variables in Data or BSS sectionDynamically allocated variables in the heapFunction parameters and local variables on the stack•Need to be able to load from and store to memoryTo manipulate the data directly in memoryOr copy the data between main memory and registers•IA-32 has many different addressing modesCorresponding to common programming constructsE.g., accessing a global variable, dereferencing a pointer, accessing a field in a struct, or indexing an array12Direct Addressing•Load or store from a particular memory locationMemory address is embedded in the instructionInstruction reads from or writes to that address•IA-32 example: movl 2000, %ecxFour-byte variable located at address 2000Read four bytes starting at address 2000Load the value into the ECX register•Useful when the address is known in advanceGlobal variables in the Data or BSS sections•Can use a label for (human) readabilityE.g., “i” to allow “movl i, %eax”13Indirect Addressing•Load or store from a previously-computed addressRegister with the address is embedded in the instructionInstruction reads from or writes to that address•IA-32 example: movl (%eax), %ecxEAX register stores a 32-bit address (e.g., 2000)Read long-word variable stored at that addressLoad the value into the ECX register•Useful when address is not known in advanceDynamically allocated data referenced by a pointerThe “(%eax)” essentially dereferences a pointer14Base Pointer Addressing•Load or store with an offset from a base addressRegister storing the base address Fixed offset also embedded in the instructionInstruction computes the address and does access•IA-32 example: movl 8(%eax), %ecxEAX 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 addressLoad the value into the ECX register•Useful when accessing part of a larger variableSpecific field within a “struct”E.g., if “age” starts at the 8th byte of “student” record15Indexed Addressing•Load or store with an offset and multiplierFixed based address embedded in the instructionOffset computed by multiplying register with constant Instruction computes the address and does access•IA-32 example: movl 2000(,%eax,4), %ecxIndex register EAX (say, with value of 10)Multiplied by a multiplier of 1, 2, 4, or 8 (say, 4)Added to a fixed base of 2000 (say, to get 2040)•Useful to iterate through an array (e.g., a[i])Base is the start of the array (i.e., “a”)Register is the index (i.e., “i”)Multiplier is the size
View Full Document