Precept 7: Introduction to IA-32 Assembly Language Programming

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
COMP 2003: Assembly Language and Digital Logic
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Flow Control Instructions
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
1 ICS 51 Introductory Computer Organization Fall 2009.
CNIT 127: Exploit Development Ch 1: Before you begin.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Computers organization & Assembly Language Chapter 1 THE 80x86 MICROPROCESSOR.
Computer Architecture and Assembly Language
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Intel MP Organization. Registers - storage locations found inside the processor for temporary storage of data 1- Data Registers (16-bit) AX, BX, CX, DX.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Chapter Overview General Concepts IA-32 Processor Architecture
CS 177 Computer Security Lecture 9
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Instruction Set Architecture
Computer Architecture and Assembly Language
Homework Reading Lab with your assigned section starts next week
Assembly language.
Credits and Disclaimers
Format of Assembly language
Homework Reading Labs PAL, pp
Aaron Miller David Cohen Spring 2011
Computer skills CPU Jakub Yaghob.
Basic Microprocessor Architecture
Assembly IA-32.
Assembly Language Programming Part 2
Homework Reading Continue work on mp1
Chapter 3 Machine-Level Representation of Programs
Symbolic Instruction and Addressing
Computer Architecture adapted by Jason Fritts then by David Ferry
Introduction to Assembly Language
Y86 Processor State Program Registers
BIC 10503: COMPUTER ARCHITECTURE
Assembly Language: IA-32 Instructions
Condition Codes Single Bit Registers
8086 Registers Module M14.2 Sections 9.2, 10.1.
Machine-Level Programming 2 Control Flow
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly Language Programming II: C Compiler Calling Sequences
Symbolic Instruction and Addressing
CS 301 Fall 2002 Computer Organization
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Practical Session 4.
Machine-Level Programming 2 Control Flow
Homework Reading Machine Projects Labs PAL, pp
The Microprocessor & Its Architecture
Symbolic Instruction and Addressing
Computer Architecture CST 250
Chapter 3 Machine-Level Representation of Programs
X86 Assembly Review.
Chapter 6 –Symbolic Instruction and Addressing
Process.
CSC 497/583 Advanced Topics in Computer Security
Chapter 8: Instruction Set 8086 CPU Architecture
Credits and Disclaimers
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Precept 7: Introduction to IA-32 Assembly Language Programming 2017. 04. 24.

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

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: https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

Memory Layout of C Program 0x00000000 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

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; }

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

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,@function 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

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

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 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 AX AH AL EBX 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 BX BH BL

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 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 CX CH CL EDX 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 DX DH DL

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 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 SI EDI 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 DI

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 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 BP ESP 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 SP

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 https://en.wikibooks.org/wiki/X86_Assembly/X86_Architecture EFLAGS 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

Instruction Pointer Register EIP Address of the next instruction to be executed EIP 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

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

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

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

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?

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:

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

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 loopend1 ## 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:

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; }

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; }

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 loopend1 ## 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:

Q&A