Machine Programming II: Instrucons Move instrucons, registers, and operands Complete addressing mode, address computaon (leal) Arithmec operaons (including some x6 6 instrucons) Condion codes Control, uncondional and condional branches While loops CSE351 Inaugural Edion Spring 2010 1 Integer Registers (IA32) %ax %cx %dx %bx %si %di %sp %bp %ah %ch %dh %bh %al %cl %dl %bl 16 bit virtual registers (backwards compability) general purpose accumulate counter data base source index destination index stack pointer base pointer Origin (mostly obsolete) CSE351 Inaugural Edion Spring 2010 2
Moving Data: IA32 Moving Data movx Source, Dest x is one of b, w, l movl Source, Dest: Move byte long word movw Source, Dest: Move 2 byte word movb Source, Dest: Move 1 byte byte Lots of these in typical code CSE351 Inaugural Edion Spring 2010 3 Moving Data: IA32 Moving Data movl Source, Dest: Operand Types Immediate: Constant integer data Example: $0x00, $-533 Like C constant, but prefixed with $ Encoded with 1, 2, or bytes Register: One of integer registers Example:, But and reserved for special use Others have special uses for parkcular instruckons Memory: consecukve bytes of memory at address given by register Simplest example: () Various other address modes CSE351 Inaugural Edion Spring 2010
movl Operand Combinaons Source Dest Src,Dest C Analog Imm Reg Mem movl $0x, temp = 0x; movl $-17,() *p = -17; movl Reg Reg Mem movl, movl,() temp2 = temp1; *p = temp; Mem Reg movl (), temp = *p; Cannot do memory memory transfer with a single instruc<on CSE351 Inaugural Edion Spring 2010 5 Simple Memory Addressing Modes Normal (R) Mem[Reg[R]] Register R specifies memory address movl (), Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset movl (), CSE351 Inaugural Edion Spring 2010 6
Using Simple Addressing Modes void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; swap: pushl movl, pushl movl (), movl (), movl (), movl (), movl,() movl,() movl -(), movl, popl ret Set Up Body Finish CSE351 Inaugural Edion Spring 2010 7 Using Simple Addressing Modes void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; swap: pushl movl, pushl movl (), movl (), movl (), movl (), movl,() movl,() movl -(), movl, popl ret Set Up Body Finish CSE351 Inaugural Edion Spring 2010
Understanding Swap void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; Register Value yp xp t1 t0 0 - yp xp Old Old Stack (in memory) movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 9 Understanding Swap yp xp 0 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx - 3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 CSE351 Inaugural Edion Spring 2010 10
Understanding Swap yp xp 0 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx - 3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 CSE351 Inaugural Edion Spring 2010 11 Understanding Swap yp xp 0-3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010
Understanding Swap 56 yp xp 0-3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 13 Understanding Swap 56 3 yp xp 0-3 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 1
Understanding Swap 56 56 3 yp xp 0-56 56 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 15 Understanding Swap 56 3 yp xp 0-56 3 Address 0x11c 0x11 0x11 0x110 0x10c 0x10 movl (), # ecx = yp movl (), # edx = xp movl (), # eax = *yp (t1) movl (), # ebx = *xp (t0) movl,() # *xp = eax movl,() # *yp = ebx CSE351 Inaugural Edion Spring 2010 16
Complete Memory Addressing Modes Most General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant displacement 1, 2, or bytes Rb: Base register: Any of integer registers Ri: Index register: Any, except for Unlikely you d use, either S: Scale: 1, 2,, or (why these numbers?) Special Cases (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]] CSE351 Inaugural Edion Spring 2010 17 Address Computaon Examples 0xf000 Expression Address Computaon Address 0x() (,) (,,) 0x0(,,2) CSE351 Inaugural Edion Spring 2010 1
Address Computaon Examples 0xf000 Expression Address Computaon Address 0x() 0xf000 + 0x 0xf00 (,) 0xf000 + 0xf100 (,,) 0xf000 + * 0xf00 0x0(,,2) 2*0xf000 + 0x0 0x1e00 CSE351 Inaugural Edion Spring 2010 19 Address Computaon Instrucon leal Src,Dest Src is address mode expression Set Dest to address denoted by expression Uses CompuKng addresses without a memory reference E.g., translakon of p = &x[i]; CompuKng arithmekc expressions of the form x + k*i k = 1, 2,, or CSE351 Inaugural Edion Spring 2010 20
Some Arithmec Operaons Two Operand Instrucons: Format Computa<on addl Src,Dest Dest = Dest + Src subl Src,Dest Dest = Dest - Src imull Src,Dest Dest = Dest * Src sall Src,Dest Dest = Dest << Src Also called shll sarl Src,Dest Dest = Dest >> Src Arithme<c shrl Src,Dest Dest = Dest >> Src Logical xorl Src,Dest Dest = Dest ^ Src andl Src,Dest Dest = Dest & Src orl Src,Dest Dest = Dest Src No disncon between signed and unsigned int (why?) CSE351 Inaugural Edion Spring 2010 21 Some Arithmec Operaons One Operand Instrucons incl Dest Dest = Dest + 1 decl Dest Dest = Dest - 1 negl Dest Dest = -Dest notl Dest Dest = ~Dest See book for more instrucons CSE351 Inaugural Edion Spring 2010 22
Using leal for Arithmec Expressions int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; arith: pushl movl, movl (), movl (), leal (,), leal (,,2), sall $, addl 16(), leal (,), imull, movl, popl ret Set Up Body Finish CSE351 Inaugural Edion Spring 2010 23 Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # What does each of ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx these instrucons = *y (t) addl 16(), # ecx = mean? z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 2
Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 25 Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 26
Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 27 Understanding arith int arith (int x, int y, int z) int t1 = x+y; int t2 = z+t1; int t3 = x+; int t = y * ; int t5 = t3 + t; int rval = t2 * t5; 16 0 z y x Old Stack movl (), # eax = x movl (), # edx = y leal (,), # ecx = x+y (t1) leal (,,2), # edx = 3*y sall $, # edx = *y (t) addl 16(), # ecx = z+t1 (t2) leal (,), # eax = +t+x (t5) imull, # eax = t5*t2 (rval) CSE351 Inaugural Edion Spring 2010 2
Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), # eax = x xorl (), # eax = x^y sarl $17, # eax = t1>>17 andl $15, # eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 29 Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), eax = x xorl (), eax = x^y (t1) sarl $17, eax = t1>>17 (t2) andl $15, eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 30
Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), eax = x xorl (), eax = x^y (t1) sarl $17, eax = t1>>17 (t2) andl $15, eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 31 Another Example int logical(int x, int y) int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; 2 13 = 192, 2 13 7 = 15 logical: pushl movl, movl (), xorl (), sarl $17, andl $15, movl, popl ret Set Up Body Finish movl (), eax = x xorl (), eax = x^y (t1) sarl $17, eax = t1>>17 (t2) andl $15, eax = t2 & 15 CSE351 Inaugural Edion Spring 2010 32