7
Practice problems 3.4 & 3.5 307 Given the following based on typedef: src_t v; dest_t *p; Implement the following operation: *p = (dest_t) v; Where v stored in %eax (or %ax or %al) pointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int movsbl %al, (%edx) char unsigned movsbl %al, (%edx) unsigned char int movzbl %al, (%edx) int char movb %al, (%edx) unsigned unsigned char movb %al, (%edx) unsigned int movl %eax, (%edx) Function with Prototype: void decode1(int *xp, int *yp, int*zp); xp=%ebp+8; yp=%ebp+12; zp=%ebp+16; Assembly code: movl 8(%ebp), %edi get xp movl 12(%ebp), %edx get yp movl 16(%ebp), %ecx get zp movl (%edx), %ebx get y movl (%ecx), %esi get z movl (%edi), %eax get x movl %eax, (%edx) store x at yp movl %ebx, (%ecx) store y at zp movl %esi, (%edi) store z at xp

Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

Practice problems 3.4 & 3.5

307

Given the following based on typedef: src_t v; dest_t *p; Implement the following operation: *p = (dest_t) v; Where v stored in %eax (or %ax or %al) pointer p is stored in register %edx

SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx)

char int movsbl %al, (%edx) char unsigned movsbl %al, (%edx)

unsigned char int movzbl %al, (%edx) int char movb %al, (%edx)

unsigned unsigned char movb %al, (%edx) unsigned int movl %eax, (%edx)

Function with Prototype:

void decode1(int *xp, int *yp, int*zp); xp=%ebp+8; yp=%ebp+12; zp=%ebp+16;

Assembly code:

movl 8(%ebp), %edi get xp

movl 12(%ebp), %edx get yp

movl 16(%ebp), %ecx get zp

movl (%edx), %ebx get y

movl (%ecx), %esi get z

movl (%edi), %eax get x

movl %eax, (%edx) store x at yp

movl %ebx, (%ecx) store y at zp

movl %esi, (%edi) store z at xp

Page 2: Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

Arithmetic and Logical Operations

Watch out for argument order! see SUB No distinction between signed and unsigned int Notice A/L for arithmetic and logical right shifts Operation Groups

Variant of the move Unary Binary Shifts

Reminder: Note the difference in instruction between assemble and disassemble – just like the movl vs mov

308

Page 3: Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

LEA – load effective address

Does not reference memory at all You don’t get the value at the address… just the address (&x)

Copies the effective address to the destination Used to generate pointers for later memory references Can also be used to compactly describe common arithmetic operations The destination operand must be a register

Example: leal 7 (%edx, %edx, 4) , %eax Sets register %eax to 5x+7 %edx + %edx*4 + 7

309

Assume: %eax = x and %ecx= y INSTRUCTION RESULT leal 6(%eax), %edx 6 + x leal (%eax, %ecx), %edx x + y leal (%eax, %ecx, 4), %edx x + 4y leal 7(%eax, %eax,8), %edx 7 + 9x leal 0xA(,%ecx,4),%edx 10 + 4y leal 9(%eax,%ecx,2), %edx 9 + x + 2y

Page 4: Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

Unary and Binary operations

Unary Single operand serves as both source and destination Register or memory location Similar to C ++ and -- operators

Binary Second operand is both source and destination Thus cannot be an

immediate value Can be memory or register First operand can be immediate, memory, or register Reminder: both cannot be memory Similar to C operations such as x += y

310

INSTRUCTION DESTINATION VALUE

addl %ecx, (%eax) 0x100 0x100

subl %edx, 4(%eax) 0x104 0xA8

imull $16,(%eax,%edx,4) 0x10C 0x110

incl 8(%eax) 0x108 0x14

decl %ecx %ecx 0x0

subl %edx, %eax %eax 0xFD

ADDRESS VALUE 0x100 0xFF 0x104 0xAB 0x108 0x13 0x10C 0x11

REGISTER VALUE

%eax 0x100

%ecx 0x1

%edx 0x3

Page 5: Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

Shift operations

Shift amount given in first operand Coded as a single byte Only shift amounts between 0 and 31 possible Only low order 5 bits are considered Immediate value or in the single byte register element %c1 (unusual!)

Value to shift in second operand Arithmetic and logical

Left shifts behave the same, though Zero fill Right shifts sign extend (arithmetic) zero fill (logical)

311

Page 6: Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

Overview

Instructions work for unsigned or two’s complement arithmetic

Except right shift Signed/unsigned division? Unsigned multiplication?

Makes 2’s comp arithmetic the preferred way to implement signed integer arithmetic

312

Page 7: Practice problems 3.4 & 3web.cse.ohio-state.edu/~reeves.92/CSE2421au12/SlidesDay38.pdfpointer p is stored in register %edx SRC_T DEST_T INSTRUCTION int int movl %eax, (%edx) char int

Arithmetic example

313

%ebp

00000000 <arith>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 4d 08 mov 0x8(%ebp),%ecx 6: 8b 55 0c mov 0xc(%ebp),%edx 9: 8d 04 52 lea (%edx,%edx,2),%eax c: c1 e0 04 shl $0x4,%eax f: 8d 44 01 04 lea 0x4(%ecx,%eax,1),%eax 13: 01 ca add %ecx,%edx 15: 03 55 10 add 0x10(%ebp),%edx 18: 0f af c2 imul %edx,%eax 1b: 5d pop %ebp 1c: c3 ret

int arith(int x, int y, int z) { int t1 = x + y; int t2 = z + t1; int t3 = x + 4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; }