The x86 Instruction Set Lecture 25 Intel Manual, Vols. 2A & 2B Robb T. Koether Hampden-Sydney College Mon, Mar 23, 2015 Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 1 / 32
1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 2 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 3 / 32
The Runtime Stack The runtime stack is a portion of memory that is used as a stack during program execution. The address of the top of the stack is stored in the register esp, called the stack pointer. The stack grows in a downward direction. When values are pushed, esp is decremented. When values are popped, esp is incremented. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 4 / 32
The Runtime Stack esp points to the top of the stack. Stack esp Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 5 / 32
The Runtime Stack Push the value 100 and decrement esp. Stack 100 esp Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 5 / 32
The Runtime Stack Push the value 200 and decrement esp again. Stack 100 200 esp Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 5 / 32
The Runtime Stack Pop a value and increment esp. Stack 100 esp Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 5 / 32
The Runtime Stack Pop another value and increment esp again. Stack esp Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 5 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 6 / 32
The Push and Pop Instructions push and pop Instructions push source pop destination where source is a register, a memory address, or an immediate value. destination is a register or a memory address. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 7 / 32
The Push and Pop Instructions The push instruction will decrement the stack pointer and then move source to the stack. The pop instruction will move the value on the stack to destination and then increment the stack pointer. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 8 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 9 / 32
Processing the Syntax Tree The syntax tree is processed in a left-to-right post-order traversal. At each node Process the left subtree. Process the right subtree. Process the node. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 10 / 32
Using the Stack As each node of the syntax tree is executed, it will leave its result on the run-time stack. The next node will pop that result off the stack (if it needs it) and then push its own result onto the stack (if there is one), and so on. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 11 / 32
Example The syntax tree for a = b + c - 5 is ASSIGN a MINUS PLUS NUM 5 DEREF DEREF b c Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 12 / 32
ASSIGN a MINUS PLUS NUM 5 DEREF DEREF b c Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
- Push the address of a. ASSIGN a MINUS PLUS NUM 5 DEREF DEREF b c addr(a) Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
ASSIGN - Push the address of a. - Push the address of b. a MINUS PLUS NUM 5 DEREF DEREF b c addr(a) addr(a), addr(b) Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
a ASSIGN MINUS - Push the address of a. - Push the address of b. DEREF - Pop the address of b, push the value of b. PLUS NUM 5 DEREF DEREF b c addr(a) addr(a), addr(b) addr(a), 10 Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
- Push the address of a. a ASSIGN MINUS - Push the address of b. DEREF - Pop the address of b, push the value of b. - Push the address of c. PLUS NUM 5 DEREF DEREF b c addr(a) addr(a), addr(b) addr(a), 10 addr(a), 10, addr(c) Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
- Push the address of a. a ASSIGN PLUS MINUS NUM 5 - Push the address of b. DEREF - Pop the address of b, push the value of b. - Push the address of c. DEREF - Pop the address of c, push the value of c. DEREF DEREF b c addr(a) addr(a), addr(b) addr(a), 10 addr(a), 10, addr(c) addr(a), 10, 20 Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
- Push the address of a. a ASSIGN PLUS MINUS NUM 5 - Push the address of b. DEREF - Pop the address of b, push the value of b. - Push the address of c. DEREF - Pop the address of c, push the value of c. PLUS - Pop two values, add them, push the result. DEREF DEREF b c addr(a) addr(a), addr(b) addr(a), 10 addr(a), 10, addr(c) addr(a), 10, 20 addr(a), 30 Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
- Push the address of a. a ASSIGN MINUS PLUS DEREF DEREF NUM 5 - Push the address of b. DEREF - Pop the address of b, push the value of b. - Push the address of c. DEREF - Pop the address of c, push the value of c. PLUS - Pop two values, add them, push the result. NUM - Push 5. b c addr(a) addr(a), addr(b) addr(a), 10 addr(a), 10, addr(c) addr(a), 10, 20 addr(a), 30 addr(a), 30, 5 Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
- Push the address of a. a ASSIGN MINUS PLUS DEREF DEREF NUM 5 - Push the address of b. DEREF - Pop the address of b, push the value of b. - Push the address of c. DEREF - Pop the address of c, push the value of c. PLUS - Pop two values, add them, push the result. NUM - Push 5. MINUS - Pop two values, subtract them, push the result. b c addr(a) addr(a), addr(b) addr(a), 10 addr(a), 10, addr(c) addr(a), 10, 20 addr(a), 30 addr(a), 30, 5 addr(a), 25 Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
- Push the address of a. a ASSIGN MINUS PLUS DEREF DEREF b c NUM 5 - Push the address of b. DEREF - Pop the address of b, push the value of b. - Push the address of c. DEREF - Pop the address of c, push the value of c. PLUS - Pop two values, add them, push the result. NUM - Push 5. MINUS - Pop two values, subtract them, push the result. ASSIGN - Pop the value and the address, store the value at the address, push the result. addr(a) addr(a), addr(b) addr(a), 10 addr(a), 10, addr(c) addr(a), 10, 20 addr(a), 30 addr(a), 30, 5 addr(a), 25 25 Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 13 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 14 / 32
A NUM Node Example (A NUM Node) push $5 A NUM node loads the integer whose value is stored in the node. For example, to load 5. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 15 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 16 / 32
An Node Example (An Node) lea a,%eax push %eax A node pushes the address of the name that is stored in the node. For example, to push the address a. The actual address of a is not known when the program is assembled. Thus, the object code does not contain the correct address of a. The address becomes known when the program is loaded into main memory to be executed. At that time, lea will produce the actual address. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 17 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 18 / 32
A DEREF Node Example (The DEREF Node) pop %eax push (%eax) A DEREF node expects to find a memory address on top of the stack. It pushes the value stored at that address. The parentheses mean the value at the address in the register. This is the indirect addressing mode. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 19 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 20 / 32
The add Instruction The add Instruction add source,destination where source is a register, a memory address, or an immediate value. destination is a register or a memory address. The value at source is added to the value at destination and the result is stored at destination Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 21 / 32
A PLUS Node Example (A PLUS Node) pop %edx pop %eax add %edx,%eax push %eax A PLUS node expects to find two numbers on the stack. The right operand should be on top. It pops the values, adds them, and pushes the result. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 22 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 23 / 32
An ASSIGN Node Example (An ASSIGN Node) pop %eax pop %edx mov %eax,(%edx) push %eax An ASSIGN node expects to find an address and a value on the stack. The value should be on top. It pops the value and the address, stores the value at the address, and pushes the value. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 24 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 25 / 32
The imul Instruction The imul instruction performs multiplication of signed integers. There are three formats. For each format, the destination is edx:eax, which holds a 64-bit value. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 26 / 32
The imul Instruction The imul Instruction (Type 1) imul source where source is one operand. eax is the other operand. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 27 / 32
The imul Instruction The imul Instruction (Type 2) imul register,source where source is a register, a memory address, or an immediate value. register is the destination. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 28 / 32
The imul Instruction The imul Instruction (Type 3) imul register,source,immediate where register is the destination. source is a register, a memory address, or an immediate value. immediate is a number. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 29 / 32
A TIMES Node Example (A TIMES Node) pop %eax pop %ecx imul %ecx push %eax A TIMES node expects to find two values on the stack. It pops them, multiplies them, and pushes the result. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 30 / 32
Outline 1 The x86 Instruction Set Push and Pop 2 Processing the AST NUM Nodes Nodes DEREF Nodes PLUS Nodes ASSIGN Nodes TIMES Nodes 3 Assignment Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 31 / 32
Assignment Assignment Read the descriptions of the above operations in the Intel Manual, Vols. 2A and 2B. Also, look up and read about subtraction and division. Robb T. Koether (Hampden-Sydney College) The x86 Instruction Set Mon, Mar 23, 2015 32 / 32