Presentation is loading. Please wait.

Presentation is loading. Please wait.

Precept 7: Introduction to IA-32 Assembly Language Programming

Similar presentations


Presentation on theme: "Precept 7: Introduction to IA-32 Assembly Language Programming"— Presentation transcript:

1 Precept 7: Introduction to IA-32 Assembly Language Programming

2 Contents Stack data structure Overview of assembly language
Memory layout Registers How to compile Convert C program into assembly language Data types in assembly language Signed vs. unsigned

3 Stack Serves as a collection of elements Two principal operations
Push: add element to collection Pop: remove most recently added element from collection Last-in-first-out (LIFO) Most recent pushed element is popped first What’s the opposite of LIFO? First-in-first-out (FIFO), e.g. Queue From:

4 Memory Layout of C Program
0x Text Code segment that contains executable instructions Data (initialized data segment) Global and static variables initialized by programmer Rodata: read-only data (e.g., string literal) Data: read-write data BSS (uninitialized data segment) Global and static variables initialized by kernel to 0 or all uninitialized global and static variables Stack Function information, local variables, etc. Heap Dynamically allocated memory (e.g., malloc) Text Rodata Data BSS Heap Stack 0xFFFFFFFF

5 Example: hello.c src/hello.c High-level language
Easy to understand by programmer Increase portability of the code to different machine architectures #include <stdio.h> int main(void) { printf(“hello, world\n”); return 0; }

6 Example: hello.s src/hello.s
Text Rodata Data BSS Heap Stack src/hello.s Code divided into sections (rodata, data, bss, text) .section “.rodata” cGreeting: .asciz “hello, world\n” ## asciz: ASCII string followed ## by zero byte. ## String “hello, world\n” ## stored in rodata section ### section “.data” ## no initialized data ### .section “.bss” ## no non-initialized data

7 We will learn details in function calls in the next precept
Example: hello.s Text Rodata Data BSS Heap Stack src/hello.s Code divided into sections (rodata, data, bss, text) .section “.text” .globl main .type main: pushl %ebp movl %esp, %ebp ## printf(“hello, world\n”); pushl $cGreeting call printf addl $4, %esp movl $0, %eax movl %ebp, %esp popl %ebp ret We will learn details in function calls in the next precept

8 Registers Small amount of storage on the CPU
Can be accessed more quickly than main memory We will use IA-32 instruction set architecture (ISA) Types of IA-32 registers 8 general purpose registers (‘E’ for extended: 32-bit) EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP 6 segment registers (not really used these days) SS, CS, DS, ES, FS, GS 1 status & control register EFLAGS 1 instruction pointer register EIP

9 General Purpose Registers (GPRs)
EAX (accumulator register) Accumulator for operands and result data. Store function’s return values EBX (base register) Accumulator for operands and result data. A function that alters EBX must restore it before returning EAX AX AH AL EBX BX BH BL

10 General Purpose Registers (GPRs)
ECX (counter register) Accumulator for operands and result data. Counter for string and loop operations EDX (data register) Accumulator for operands and result data. I/O pointer ECX CX CH CL EDX DX DH DL

11 General Purpose Registers (GPRs)
ESI (source index register) Accumulator for operands and result data. Source pointer for string operations. A function that alters ESI must restore it before returning. EDI (destination index register) Accumulator for operands and result data. Destination pointer for string operations. A function that alters EDI must restore it before returning. ESI SI EDI DI

12 General Purpose Registers (GPRs)
EBP (stack base pointer register) Pointer to bottom of current stack frame ESP (stack pointer register) Pointer to top of current stack frame EBP BP ESP SP

13 Status & Control Register
EFLAGS Condition code bits indicating the result of the most recent cmp instruction (and other instructions), and other control and status bits. Example Bit 0 (CF): carry flag Set if the last arithmetic operation carried (addition) or borrowed (subtraction) a bit beyond the size of the register Details in EFLAGS

14 Instruction Pointer Register
EIP Address of the next instruction to be executed EIP

15 How to compile for IA-32 [IMPORTANT] Set your 64-bit Ubuntu to support 32-bit program $ sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 g++-multilib Compile hello.c to hello.s using gcc209 $ gcc209 –m32 –march=i386 hello.c -S Use as command to make object file from hello.s file $ as --32 –march=i386 hello.s –o hello.o Use gcc209 to link library $ gcc209 –m32 -march=i386 hello.o –o hello Compile hello.c with debugging symbols. Compare hello.s files $ gcc209 –m32 -march=i386 hello.c –S –g

16 Data Types in Assembly Language
C has data types Use keyword to define data types (e.g., signed/unsigned) Assembly language does not have data types Use separate instructions for different data types It’s your responsibility: to keep track of the data types in memory (or register) to use proper instructions based on the data types

17 Instructions for Data Types
Multiplication Signed: imul(b/w/l) Unsigned: mul(b/w/l) Division Signed: idiv(b/w/l) Unsigned: div(b/w/l) Control transfer (jump after compare) Signed: je (equal to), jne, jl (less than), jle, jg (greater than), jge Unsigned: je, jne, jb (below), jbe, ja (above), jae b (byte): 8-bit w (word): 16-bit l (long): 32-bit

18 Example: power.c src/power.c
Reads two integers (iBase, iExp)  computes (iBase ^ iExp) #include <stdio.h> static int iBase, iExp, iIndex, iPower = 1; int main(void) { scanf("%d", &iBase); // Get base value scanf("%d", &iExp); // Get exponent value // Compute iBase to the iExp power for (iIndex = 1; iIndex <= iExp; iIndex++) iPower *= iBase; printf("%d to the %d power is %d.\n", iBase, iExp, iPower); return 0; } Stored in DATA section Stored in BSS section We will learn details in function calls in the next precept How do we convert this to assembly language?

19 Flattened C Program Flatten loop by using “goto” labels (src/powerflat.c) // Compute iBase to the iExp power for (iIndex = 1; iIndex <= iExp; iIndex++) iPower *= iBase; // Compute iBase to the iExp power iIndex = 1; loop1: if (iIndex > iExp) goto loopend1; iPower *= iBase; iIndex++; goto loop1; loopend1:

20 Allocate long type memory (4B in IA-32) and set to 1
Example: power.s src/power.s ### .section ".data"  iPower: .long 1  ###   .section ".bss"  iBase: .skip 4  iExp: iIndex: Allocate long type memory (4B in IA-32) and set to 1 Allocate space of 4B

21 Example: power.s src/power.s … loop1:
## if (iIndex > iExp) goto loopend1 movl iIndex, %eax ## Set %eax value to be iIndex cmpl iExp, %eax ## Compare iExp and %eax (=iIndex) jg loopend ## If %eax is greater than iExp, jump to loopend1 ## iPower *= iBase movl iPower, %eax ## Set %eax value to be iPower imull iBase ## Signed multiplication. %eax = %eax * iBase movl %eax, iPower ## Update iPower value to be %eax value ## iIndex++ incl iIndex ## goto loop1 jmp loop1 loopend1:

22 Unsigned Version src/powerunsigned.c Use unsigned integers
#include <stdio.h> static unsigned int uiBase, uiExp, uiIndex, uiPower = 1; int main(void) { scanf("%u", &uiBase); // Get base value scanf("%u", &uiExp); // Get exponent value // Compute iBase to the iExp power for (uiIndex = 1; uiIndex <= uiExp; uiIndex++) uiPower *= uiBase; printf("%u to the %u power is %u.\n", uiBase, uiExp, uiPower); return 0; }

23 Unsigned Version Flattened
src/powerunsignedflat.c Use unsigned integers #include <stdio.h> static unsigned int uiBase, uiExp, uiIndex, uiPower = 1; int main(void) { scanf("%u", &uiBase); // Get base value scanf("%u", &uiExp); // Get exponent value loop1: if (uiIndex > uiExp) goto loopend1; uiPower *= uiBase; uiIndex++; goto loop1; loopend1: printf("%u to the %u power is %u.\n", uiBase, uiExp, uiPower); return 0; }

24 Unsigned Version Assembly Language
demo/flattenedc/powerunsigned.s movl $1, uiIndex ## uiIndex = 1 loop1: ## if (uiIndex > uiExp) goto loopend1 movl uiIndex, %eax ## Set %eax value to be uiIndex cmpl uiExp, %eax ## Compare uiExp and %eax (=uiIndex) ja loopend ## If %eax is above uiExp, jump to loopend1 ## uiPower *= uiBase movl uiPower, %eax ## Set %eax value to be uiPower mull uiBase ## Unsigned multiplication. %eax = %eax * uiBase movl %eax, uiPower ## Update uiPower value to be %eax value ## uiIndex++ incl uiIndex ## goto loop1 jmp loop1 loopend1:

25 Q&A


Download ppt "Precept 7: Introduction to IA-32 Assembly Language Programming"

Similar presentations


Ads by Google