Lecture11 cs341.pdf
Document Details
Uploaded by DesirousChocolate
UMass Boston
Tags
Full Transcript
Assembly Language Programming V: In - line Assembly Code 1 2 Homework • Reading – PAL, pp 3 -6, 361 -367 3 In - line Assembly Code • The gcc compiler allows you to put assembly instructions in - line between your C statements • This is a lot trickier way to integrate assembly code with C...
Assembly Language Programming V: In - line Assembly Code 1 2 Homework • Reading – PAL, pp 3 -6, 361 -367 3 In - line Assembly Code • The gcc compiler allows you to put assembly instructions in - line between your C statements • This is a lot trickier way to integrate assembly code with C than writing C callable functions • You need to know a lot more about how the compiler locates variables, uses registers, and manages the stack. • Does execute faster than a function call to the equivalent amount of assembly code 4 In - line Assembly Code • Example Function with in -line assembly code int foobar(int x, int *y) { int i, *j; asm("pushl %eax"); i = x; j = &i; *y = *j; asm("popl %eax"); return 0; } 5 Assembled Code • Resulting .s file at entry point: _foobar: pushl %ebp movl %esp,%ebp subl $8,%esp # space for automatic variables pushl %ebx # will use %ebx for pointers #APP pushl %eax #NO_APP … 6 Assembled Code(cont’d) • State of the Stack at maximum depth: y x %eip %ebp i j %ebx %eax %ebp %esp Argument Variables (positive offsets from %ebp) Automatic Variables (negative offsets from %ebp) 7 Assembled Code(cont’d) • Body of function logic movl 8(%ebp),%eax # i = x; movl %eax, -4(%ebp) leal -4(%ebp),%ebx # j = &i; movl %ebx, -8(%ebp) movl 12(%ebp),%eax # *y = *j; movl -8(%ebp),%edx movl (%edx),%ecx movl %ecx,(%eax) 8 Assembled Code(cont’d) • Resulting .s file at return point: #APP popl %eax #NO_APP xorl %eax,%eax # clear %eax for return 0; jmp L1 .align 4,0x90 # align to a 4 -byte boundary and pad the # spaces with NOPs L1: movl -12(%ebp),%ebx leave # translates to instructions below # movl %ebp, %esp # popl %ebp ret 9 Machine Language Lets look at our disassembly (objdump) of tiny.lnx itserver6% objdump -S tiny.lnx tiny.lnx: file format elf32 -i386 Disassembly of section .text: 00100100 <_start>: 100100: b8 08 00 00 00 mov $0x8,%eax 100105: 83 c0 03 add $0x3,%eax 100108: a3 00 02 00 00 mov %eax,0x200 10010d: cc int 3 10 Machine Language • Another way to show the same data itserver6$ as -- 32 -al -o tiny.o tiny.s GAS LISTING tiny.s page 1 1 # tiny.s: mp2warmup program 2 3 .globl _start 4 _start: 5 0000 B8080000 movl $8, %eax 5 00 6 0005 83C003 addl $0x3, %eax 7 0008 A3000200 movl %eax, 0x200 7 00 8 000d CC int $3 9 .end • How do we understand the hex code values? • We need to understand the machine language coding rules! – Built up using various required and optional binary fields – Each field has a unique location, size in bits, and meaning for code values 11 Machine Language • The i386 byte by byte binary instruction format is shown in the figure with fields: – Optional instruction prefix – Operation code (op code) – Optional Modifier, Scale -Index -Byte (SIB) – Optional Data Elements Instruction Prefixes Opcode ModR/M SIB Displace - ment Data Elements Modifiers 0-4 Bytes 1-3 Bytes 0-1 Bytes 0-1 Bytes 0-4 Bytes 0-4 Bytes Optional Instruction Prefixes • You can find details in: http://www.c -jump.com/CIS77/CPU/x86/X77_0240_prefix.htm 12 13 Machine Language • Binary Machine Coding for Some Sample Instructions Opcode ModR/M Data Total movl reg, reg 10001001 11sssddd none 2 movl idata, reg 10111ddd idata 5 addl reg, reg 00000001 11sssddd none 2 addl idata, reg 10000001 11000ddd* idata 6 subl reg, reg 00101001 11sssddd none 2 subl idata, reg 10000001 11101ddd* idata 6 incl reg 01000ddd 1 decl reg 01001ddd 1 14 Machine Language • ModR/M 3 - Bit Register Codes (for sss or ddd) %eax 000 %esp 100 %ecx 001 %ebp 101 %edx 010 %esi 110 %ebx 011 %edi 111 • * Optimization: For some instructions involving %eax, there is a shorter machine code available (hence prefer %eax) 15 Machine Language • Examples from tiny.lnx: b8 08 00 00 00 movl $0x8,%eax b8 = 1011 1ddd with ddd = 000 for %eax 08 00 00 00 = immediate data for 0x00000008 83 c0 03 addl $0x3,%eax (See Note) 83 = opcode c0 = 11000ddd with ddd = 000 for %eax 03 = short version of immediate data value Note: Shorter than 81 c0 03 33 33 33 if idata =0x3333 16 Machine Language • Why should you care about machine language? • Generating optimal code for performance!! • Example: b8 00 00 00 00 movl $0, %eax # clear %eax Generates five bytes 31 c0 xorl %eax, %eax # clear %eax Generates two bytes • Two bytes uses less program memory and is faster to fetch and execute than five bytes!!