Presentation is loading. Please wait.

Presentation is loading. Please wait.

IA32 Addressing Modes Chapter 5 The ISA Level cont’d.

Similar presentations


Presentation on theme: "IA32 Addressing Modes Chapter 5 The ISA Level cont’d."— Presentation transcript:

1 IA32 Addressing Modes Chapter 5 The ISA Level cont’d.

2 addressing Opcode indicates what operation is to be performed. Operands specify the location of data. Example of addressing modes that we have already used: A=12 Bdword52 moveax, 1; immediate movebx, eax; register movecx, A; immediate movedx, B; direct memory

3 IA32 addressing modes 1.Immediate 2.Direct memory 3.Register 4.Register indirect(new) 5.Indexed(new) 6.Based-indexed(new)

4 REGISTER INDIRECT

5 Register indirect Consider the following: Bdword52 … moveax, B ; eax now equals 52 In the above example, the operand actually contains the address of B. That’s not very flexible.

6 Register indirect Say B is located at memory location (address) 400. We load the address of B into a register. movebx, 400;load the address of B moveax, ebx;eax is now 400 moveax, [ebx];eax now equals 52 This is the register indirect addressing mode. The register does not contain the value but contains a reference to (pointer to/location of) the value in memory.

7 Register indirect But how do we get the address of something into a register to start with? We don’t know where the assembler and linker place A in memory! Just as we have instructions that load values from memory, we have instructions that load the address of values in memory. leaeax, A;store addr of A in eax ;lea = load effective addr moveax, offset A;same result

8 HOW CAN WE USE THE REGISTER INDIRECT ADDRESSING MODE TO SUM UP THE VALUES IN A TABLE?

9 Sum up values in a table tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;eax points to the first table entry movebx, [eax];ebx contains the first table entry ;how can we make eax point to the next table entry?

10 Sum up values in a table tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;eax points to the first table entry movebx, [eax];ebx contains the first table entry addeax, 4;eax now points to next table entry

11 Sum up values in a table tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;eax points to the first table entry movebx, [eax];ebx contains the first table entry addeax, sizeof dword;even better

12 Sum up values in a table tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;eax points to the first table entry movebx, [eax];ebx contains the first table entry addeax, 4;eax now points to next table entry ;how can we sum the next entry?

13 Sum up values in a table tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;eax points to the first table entry movebx, [eax];ebx contains the first table entry addeax, 4;eax now points to next table entry addebx, [eax]; sum the second entry …

14 Sum up values in a table tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;eax points to the first table entry movebx, [eax];ebx contains the first table entry addeax, 4;eax now points to next table entry addebx, [eax];sum the second entry addeax, 4 addebx, [eax];sum the third addeax, 4 addebx, [eax];sum the fourth addeax, 4 addebx, [eax];sum the fifth addeax, 4 addebx, [eax];sum the sixth Note: If we know that we have N entries in the table, we can use a loop instead.

15 INDEXED

16 Indexed Used to: 1.reference memory at a constant offset from a register when register points to object and data member is a know offset from start of object 2.reference memory using a register as an additional offset when register is used as an offset to an array element

17 Indexed Adword592h, 50h, 60h, 70h, 80h, 90h moveax, 4 movebx, A[eax];what’s in ebx? movecx, [A+eax];what’s in ecx?

18 Indexed Adword592h, 50h, 60h, 70h, 80h, 90h moveax, 4 movebx, A[eax];what’s in ebx? movecx, [A+eax];what’s in ecx?

19 Using indexed addressing to sum up values in a table tbldword1, 2, 7, 22, 59, 100 moveax, 0;eax is offset from start of table movebx, tbl[eax];ebx contains the first table entry addeax, 4;eax now is offset to next table entry addebx, tbl[eax];sum the second entry addeax, 4 addebx, tbl[eax];sum the third addeax, 4 addebx, tbl[eax];sum the fourth addeax, 4 addebx, tbl[eax];sum the fifth addeax, 4 addebx, tbl[eax];sum the sixth

20 BASED-INDEXED

21 Based-indexed Address is computed by adding two registers (base and index) plus an optional offset (displacement). One of the two registers (but not both) may also be optionally scaled. Offset = base + (index*scale) + displacement

22 Based-indexed

23 Using based-indexed addressing to sum up values in a table (approach #1) tbldword1, 2, 7, 22, 59, 100 moveax, 0;offset from start of table movebx, tbl[eax*4];get first table entry inceax;subscript of next table entry addebx, tbl[eax*4];sum the second entry inceax addebx, tbl[eax*4];sum the third inceax addebx, tbl[eax*4];sum the fourth inceax addebx, tbl[eax*4];sum the fifth inceax addebx, tbl[eax*4];sum the sixth

24 Using based-indexed addressing to sum up values in a table (approach #2) tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;point to start of table movebx, 0;offset from start of table movecx, [eax+ebx*4];get first table entry incebx;subscript of next table entry addecx, [eax+ebx*4];sum the second entry incebx addecx, [eax+ebx*4];sum the third incebx addecx, [eax+ebx*4];sum the fourth incebx addecx, [eax+ebx*4];sum the fifth incebx addecx, [eax+ebx*4];sum the sixth

25 Using based-indexed addressing to sum up values in a table (approach #3) tbldword1, 2, 7, 22, 59, 100 moveax, offset tbl;points to start of table movebx, 0;offset from start of table movecx, [eax+ebx];get first table entry addebx, 4;offset of next table entry addecx, [eax+ebx];sum the second entry addebx, 4 addecx, [eax+ebx];sum the third addebx, 4 addecx, [eax+ebx];sum the fourth addebx, 4 addecx, [eax+ebx];sum the fifth addebx, 4 addecx, [eax+ebx];sum the sixth

26 USING THESE ADDRESSING MODES

27 Methods of passing arguments to functions Methods: 1.Use registers. 2.Use stack. 3.Use one register which points to a parameter block.

28 Stack temporarily keeps return address. mainPROC;program execution begins here moveax, offset afterCall;addr of inst after call f calldump;show contents of regs callf afterCall: moveax, input(prompt);prompt the user exit;end of program mainENDP ;---------------------------------------------------------------------- fPROC moveax, [esp];gets return address calldump ret fENDP

29 mainPROC;program execution begins here moveax, offset afterCall;addr of inst after call f calldump;show contents of regs callf afterCall: moveax, input(prompt);prompt the user exit;end of program mainENDP ;---------------------------------------------------------------------- fPROC moveax, [esp];gets addr of return address calldump ret fENDP Note: In f, esp points to return address which is location of afterCall.

30 mainPROC;program execution begins here moveax, 7 pusheax;push the contents of eax on the stack push4;push the value 4 on the stack callf popeax;must clean up stack! moveax, input(prompt);prompt the user exit;end of program mainENDP ;---------------------------------------------------------------------- fPROC moveax, [esp+4];eax = 4 movebx, [esp+8];ebx = 7 calldump ret fENDP Passing arguments to functions via the stack

31 mainPROC;program execution begins here moveax, 7 pusheax;push the contents of eax on the stack push4;push the value 4 on the stack callf popeax;must clean up stack! moveax, input(prompt);prompt the user exit;end of program mainENDP ;---------------------------------------------------------------------- fPROC moveax, [esp+4];eax = 4 movebx, [esp+8];ebx = 7 calldump ret fENDP Passing arguments to functions via the stack Can you suggest another way to perform this clean up?

32 Passing arguments to functions via the stack mainPROC;program execution begins here moveax, 7 pusheax;push the contents of eax on the stack push4;push the value 4 on the stack callf addesp, 8;must clean up stack! moveax, input(prompt);prompt the user exit;end of program mainENDP ;---------------------------------------------------------------------- fPROC moveax, [esp+4];eax = 4 movebx, [esp+8];ebx = 7 calldump ret fENDP Another way to perform this clean up.

33 Passing arguments to functions via the stack mainPROC;program execution begins here moveax, 7 pusheax;push the contents of eax on the stack push4;push the value 4 on the stack callf addesp, 8;must clean up stack! moveax, input(prompt);prompt the user exit;end of program mainENDP ;---------------------------------------------------------------------- fPROC moveax, [esp+4];eax = 4 movebx, [esp+8];ebx = 7 calldump ret fENDP But this functions modifies eax and ebx. How can we modify this function to NOT change any registers?

34 Passing arguments to functions via the stack fPROC pusheax;save reg used pushebx;save reg used moveax, [esp+12];get arg movebx, [esp+16];get another arg. popebx;restore reg used popeax;restore reg used ret fENDP We modified this to NOT change any registers.

35 CALCULATING FACTORIAL

36 n! (n factorial) The number of ways n objects can be permuted (arranged). For example, consider 3 things, A, B, and C. 3! = 6 1.ABC 2.ACB 3.CAB 4.CBA 5.BCA 6.BAC The first few factorials for n = 0, 1, 2, 3, 4, 5 are 1, 1, 2, 6, 24, 120.

37 n! (n factorial) n! for some non negative integer n is defined as: – n! = n * (n-1) * (n-2) * … * 2 * 1 – 0! is defined as 1. – from http://mathworld.wolfram.com/Factorial.htmlhttp://mathworld.wolfram.com/Factorial.html

38 Java version (iterative) public static int fact1 ( int n ) { int result = 1; for (int ecx=n; ecx>0; ecx--) { result = result * ecx; } return result; }

39 fact1proc;input:eax contains n (value to calculate n!) ;output:eax contains the result (n!) ;all other registers except edx are preserved pushecx;save register used movecx, eax;init loop counter to n moveax, 1;to accumulate result (also n! = 1 for n<=1) cmpecx, 1;check for 1! jlefact1Done;br if 1! fact1Loop: mulecx;accumulate the result of n! jofact1Error;br if result is larger than 32 bits dececx;dec our loop counter jnzfact1Loop;br if not finished fact1Done: popecx;restore register used ret;return to caller fact1Error:;handle errors pushad;save all registers printSADD("fact1: overflow",CR,LF);output error message popad;restore all registers popecx;restore register used ret;return to caller fact1endp Iterative factorial Two things to note: 1.This is a common version of the for-loop. 2.How can we also preserve edx? (Where does edx change?)

40 BUT CAN WE DO IT RECURSIVELY?

41 n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1

42 Mathematical induction The idea of sequences in which later terms are deduced from earlier ones, which is implicit in the principle of mathematical induction, dates to antiquity. The truth of an infinite sequence of propositions P i for i=1,...,  is established if 1.P 1 is true, and 2.P k implies P k+1 for all k. This principle is sometimes also known as the method of induction. – from http://mathworld.wolfram.com/RecursiveSequence.html and http://mathworld.wolfram.com/PrincipleofMathematicalInduction. htmlhttp://mathworld.wolfram.com/RecursiveSequence.html http://mathworld.wolfram.com/PrincipleofMathematicalInduction. html

43 Mathematical induction The idea of sequences in which later terms are deduced from earlier ones, which is implicit in the principle of mathematical induction, dates to antiquity. The truth of an infinite sequence of propositions P i for i=1,...,  is established if 1.P 1 is true, and 2.P k implies P k+1 for all k. base case(s) inductive case(s)

44 Back to n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1 base cases inductive case

45 Let’s code n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { } base cases inductive case

46 Let’s code n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base cases if (n==0)return 1; } base cases inductive case

47 Let’s code n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base cases if (n==0)return 1; if (n==1)return 1; } base cases inductive case

48 Let’s code n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base case if (n<=1)return 1; //more generic/robust } base cases inductive case

49 Let’s code n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base case if (n<=1)return 1; return n * nFactorial( n-1 ); } base cases inductive case

50 Let’s code n! (n factorial) n! for some non negative integer n can be rewritten as: – 0! = 1for n = 0 – 1! = 1for n = 1 – n! = n * (n-1)! for all other n > 1 public static int nFactorial ( int n ) { //base case if (n<=1)return 1; return n * nFactorial( n-1 ); } This is an example of a recursive function (a function that calls itself)! To use this function: int result = nFactorial( 10 );

51 fact2proc;input:eax contains n (value to calculate n!) ;output:eax contains the result (n!) ;all other registers except edx are preserved cmpeax, 1;base case when eax=1 jlefact2One;br if 1 (actually <=1) pushebx;save reg used movebx, eax;save copy of n so we can calculate (n-1)! deceax;get ready to calculate (n-1)! callfact2;calculate (n-1)! mulebx;calculate n*[(n-1)!] jofact2Error;br if result is larger than 32 bits popebx;restore reg used ret;return to caller fact2One: moveax, 1;1! = 1 ret;return to caller fact2Error:;handle errors pushad;save all registers printSADD("fact2: overflow",CR,LF);output error message popad;restore all registers popebx;restore reg used ret;return to caller fact2endp Recursive factorial

52 Summary of IA32 addressing modes 1. Immediatemoveax, 12 2. Direct memorymoveax, X 3. Registermovebx, eax 4. Register indirectmovebx, [eax] 5. Indexedmovebx, T[eax] movebx, [T+eax] 6. Based-indexedmovecx, [eax + ebx*2 + T]

53 Methods of passing arguments to functions Methods: 1.Use registers. 2.Use stack. 3.Use one register which points to a parameter block.

54 .data Point3Dstruct//structure defn. xdword0 ydword0 zdword0 Point3Dends p1Point3D{ }//p1 w/ default values p2Point3D{1, 2, 3}//p2 w/ specified values

55 .data Point3Dstruct//structure defn. xdword0 ydword0 zdword0 Point3Dends p1Point3D{ }//p1 w/ default values p2Point3D{1, 2, 3}//p2 w/ specified values moveax, p2.x;get x value movebx, p2.y;get y value movecx, p2.z;get z value calldump

56 .data Point3Dstruct//structure defn. xdword0 ydword0 zdword0 Point3Dends p1Point3D{ }//p1 w/ default values p2Point3D{1, 2, 3}//p2 w/ specified values leaeax, p2;get addr of p2 movebx, offset p2;get addr of p2 via another method calldump

57 .data Point3Dstruct//structure defn. xdword0 ydword0 zdword0 Point3Dends p1Point3D{ }//p1 w/ default values p2Point3D{1, 2, 3}//p2 w/ specified values leaeax, p2;get addr of p2 movebx, offset p2;get addr of p2 via another method calldump movPoint3D.x[eax], 5 movPoint3D.z[eax], 10 moveax, p2.x;get x movebx, p2.y;get y movecx, p2.z;get z calldump eax contains the base address of structured data that can be referenced by called functions! (Point3D.x is simply the offset to x from the start of the struct.)


Download ppt "IA32 Addressing Modes Chapter 5 The ISA Level cont’d."

Similar presentations


Ads by Google