Rutgers University Santosh Nagarakatte 131 Rfact Body Registers ebx Stored

# Rutgers university santosh nagarakatte 131 rfact body

This preview shows page 131 - 141 out of 155 pages.

Rutgers University Santosh Nagarakatte 131 Rfact Body Registers %ebx Stored value of x %eax l Temporary value of x-1 l Returned value from rfact(x-1) l Returned value from this call movl 8(%ebp),%ebx # ebx = x cmpl \$1,%ebx # Compare x : 1 jle .L78 # If <= goto Term leal -1(%ebx),%eax # eax = x-1 pushl %eax # Push x-1 call rfact # rfact(x-1) imull %ebx,%eax # rval * x jmp .L79 # Goto done .L78: # Term: movl \$1,%eax # return val = 1 .L79: # Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x ; } Recursion
Rfact Recursion x Rtn adr Old %ebp %ebp Old %ebx pushl %eax %esp x-1 x-1 %eax x %ebx x Rtn adr Old %ebp %ebp Old %ebx %esp %eax x %ebx x-1 leal -1(%ebx),%eax x Rtn adr Old %ebp %ebp Old %ebx x-1 x-1 %eax x %ebx %esp Rtn adr call rfact
Rfact Result x Rtn adr Old %ebp %ebp Old %ebx %esp x-1 imull %ebx,%eax x! %eax x %ebx x Rtn adr Old %ebp %ebp Old %ebx %esp x-1 %eax x %ebx Return from Call (x-1)! Assume that rfact(x-1) returns (x-1)! in register %eax
Rfact Completion x Rtn adr Old %ebp %ebp 0 4 8 Old %ebx %esp -4 x! %eax %ebx x-1 -8 pre %ebp pre %ebx x Rtn adr Old %ebp %ebp 0 4 8 %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl % ebp ret x Rtn adr %ebp %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx
Rutgers University Santosh Nagarakatte 135 Basic Data Types Integral n Stored & operated on in general registers n Signed vs. unsigned depends on instructions used Intel GAS Bytes C byte b 1 [ unsigned ] char word w 2 [ unsigned ] short double word l 4 [ unsigned ] int Floating Point n Stored & operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10/12 long double
Rutgers University Santosh Nagarakatte 136 Array Allocation Basic Principle T A[ L ]; n Array of data type T and length L n Contiguously allocated region of L * sizeof( T ) bytes char string[12]; x x + 12 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 double a[4]; x + 32 x + 24 x x + 8 x + 16 char *p[3]; x x + 4 x + 8
Rutgers University Santosh Nagarakatte 137 Array Example Notes n Declaration “ zip_dig cmu ” equivalent to “ int cmu[5] n Example arrays were allocated in successive 20 byte blocks l Not guaranteed to happen in general typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig mit; 0 2 1 3 9 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 0 56 60 64 68 72 76
Rutgers University Santosh Nagarakatte 138 Array Accessing Example Memory Reference Code int get_digit (zip_dig z, int dig) { return z[dig]; } # %edx = z # %eax = dig movl (%edx,%eax,4),%eax # z[dig] Computation n Register %edx contains starting address of array n Register %eax contains array index n Desired digit at 4*%eax + %edx n Use memory reference (%edx, %eax,4)
Rutgers University Santosh Nagarakatte 139 Referencing Examples Code Does Not Do Any Bounds Checking! Reference Address Value Guaranteed? mit[3] 36 + 4* 3 = 48 3 mit[5] 36 + 4* 5 = 56 9 mit[-1] 36 + 4*-1 = 32 3 cmu[15] 16 + 4*15 = 76 ?? n Out of range behavior implementation-dependent l No guaranteed relative allocation of different arrays zip_dig cmu; 1 5 2 1 3 16 20 24 28 32 36 zip_dig mit; 0 2 1 3 9 36 40 44 48 52 56 zip_dig ucb; 9 4 7 2 0 56 60 64 68 72 76 Yes No No No
Rutgers University Santosh Nagarakatte 140 int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; } Array Loop Example Original Source int zd2int(zip_dig z) { int zi = 0; int *zend = z + 4;

#### You've reached the end of your free preview.

Want to read all 155 pages?

• Fall '15
• SantoshNagarakatte
• Computer Architecture, Processor register, Call stack, Addressing mode, eax, Prof. Santosh Nagarakatte, Santosh Nagarakatte