Machine Level Programming I: Introduction - Lecture Notes | CSCE 230, Study notes of Computer Architecture and Organization

Material Type: Notes; Class: Computer Organization; Subject: Computer Science and Engineering ; University: University of Nebraska - Lincoln; Term: Unknown 1993;

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-1uv-2
koofers-user-1uv-2 🇺🇸

8 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1
Machine-Level Programming I:
Introduction
Machine-Level Programming I:
Introduction
CSCE 230J
Computer Organization
Dr. Steve Goddard
http://cse.unl.edu/~goddard/Courses/CSCE230J
2
Giving credit where credit is due
Giving credit where credit is due
Most of slides for this lecture are based on
slides created by Drs. Bryant and
O’Hallaron, Carnegie Mellon University.
I have modified them and added new
slides.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Machine Level Programming I: Introduction - Lecture Notes | CSCE 230 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Machine-Level Programming I:

Introduction

Machine-Level Programming I:

Introduction

CSCE 230J

Computer Organization

Dr. Steve Goddard

[email protected]

http://cse.unl.edu/~goddard/Courses/CSCE230J

2

Giving credit where credit is due Giving credit where credit is due

 Most of slides for this lecture are based on

slides created by Drs. Bryant and

O’Hallaron, Carnegie Mellon University.

 I have modified them and added new

slides.

3

Topics Topics

Assembly Programmer’s Execution

Model

Accessing Information

Registers

Memory

Arithmetic operations

4

IA32 Processors IA32 Processors

Totally Dominate Computer MarketTotally Dominate Computer Market

Evolutionary DesignEvolutionary Design

 Starting in 1978 with 8086

 Added more features as time goes on

 Still support old features, although obsolete

Complex Instruction Set Computer (CISC)Complex Instruction Set Computer (CISC)

 Many different instructions with many different formats

 But, only small subset encountered with Linux programs

 Hard to match performance of Reduced Instruction Set

Computers (RISC)

 But, Intel has done just that!

7

X86 Evolution: Programmer’s View X86 Evolution: Programmer’s View

Name Name DateDate (^) TransistorsTransistors

Pentium IIIPentium III^19991999 8.2M8.2M

 Added “streaming SIMD” instructions for operating on 128-bit

vectors of 1, 2, or 4 byte integer or floating point data

 Our fish machines

Pentium 4Pentium 4^20012001 42M42M

 Added 8-byte formats and 144 new instructions for streaming

SIMD mode

8

X86 Evolution: Clones X86 Evolution: Clones

Advanced Micro Devices (AMD)Advanced Micro Devices (AMD)

 Historically

AMD has followed just behind Intel A little bit slower, a lot cheaper

 Recently

Recruited top circuit designers from Digital Equipment Corp. Exploited fact that Intel distracted by IA Now are close competitors to Intel

 Developing own extension to 64 bits

9

X86 Evolution: Clones X86 Evolution: Clones

TransmetaTransmeta

 Recent start-up

Employer of Linus Torvalds

 Radically different approach to implementation

Translates x86 code into “Very Long Instruction Word” (VLIW) code High degree of parallelism

 Shooting for low-power market

10

New Species: IA64 New Species: IA

Name Name DateDate (^) TransistorsTransistors

ItaniumItanium^20012001 10M10M

 Extends to IA64, a 64-bit architecture

 Radically new instruction set designed for high performance

 Will be able to run existing IA32 programs

On-board “x86 engine”

 Joint project with Hewlett-Packard

ItaniumItanium 22 20022002 221M221M

 Big performance boost

13

Compiling Into Assembly Compiling Into Assembly

C Code C Code

int sum(int x, int y) { int t = x+y; return t; }

Generated Assembly

_sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax movl %ebp,%esp popl %ebp ret

Obtain with command gcc -O -S code.c

Produces file code.s

14

Assembly Characteristics Assembly Characteristics

Minimal Data TypesMinimal Data Types

 “Integer” data of 1, 2, or 4 bytes

 Data values  Addresses (untyped pointers)

 Floating point data of 4, 8, or 10 bytes

 No aggregate types such as arrays or structures

 Just contiguously allocated bytes in memory

Primitive OperationsPrimitive Operations

 Perform arithmetic function on register or memory data

 Transfer data between memory and register

 Load data from memory into register  Store register data into memory

 Transfer control

 Unconditional jumps to/from procedures  Conditional branches

15

Code for sum

0x401040 : 0x 0x 0xe 0x8b 0x 0x0c 0x 0x 0x 0x 0xec 0x5d 0xc

Object Code Object Code AssemblerAssembler

 Translates .s into .o

 Binary encoding of each instruction

 Nearly-complete image of executable

code

 Missing linkages between code in

different files

Linker Linker

 Resolves references between files

 Combines with static run-time

libraries

 E.g., code for malloc , printf

 Some libraries are dynamically linked

 Linking occurs when program begins execution

  • Total of 13 bytes
  • Each instruction 1, 2, or 3 bytes
  • Starts at address 0x

16

Machine Instruction Example Machine Instruction Example C Code C Code

 Add two signed integers

Assembly Assembly

 Add 2 4-byte integers

“Long” words in GCC parlance Same instruction whether signed or unsigned

 Operands:

x : Register %eax y : Memory M[ %ebp+8] t : Register %eax » Return function value in %eax

Object Code Object Code

 3-byte instruction

 Stored at address 0x

int t = x+y;

addl 8(%ebp),%eax

0x401046: 03 45 08

Similar to expression x += y

19

What Can be Disassembled? What Can be Disassembled?

 Anything that can be interpreted as executable code

 Disassembler examines bytes and reconstructs assembly

source

% objdump -d WINWORD.EXE

WINWORD.EXE: file format pei-i

No symbols in "WINWORD.EXE". Disassembly of section .text:

30001000 <.text>: 30001000: 55 push %ebp 30001001: 8b ec mov %esp,%ebp 30001003: 6a ff push $0xffffffff 30001005: 68 90 10 00 30 push $0x 3000100a: 68 91 dc 4c 30 push $0x304cdc

20

Moving Data Moving Data

Moving DataMoving Data

movl Source , Dest :

 Move 4-byte (“long”) word

 Lots of these in typical code

Operand TypesOperand Types

 Immediate: Constant integer data

 Like C constant, but prefixed with ‘ $ ’  E.g., $0x400 , $-  Encoded with 1, 2, or 4 bytes

 Register: One of 8 integer registers

 But %esp and %ebp reserved for special use  Others have special uses for particular instructions

 Memory: 4 consecutive bytes of memory

 Various “address modes”

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

21

movl movl Operand CombinationsOperand Combinations

 Cannot do memory-memory transfers with single

instruction

movl

Imm

Reg

Mem

Reg

Mem

Reg

Mem

Reg

Source Destination

movl $0x4,%eax

movl $-147,(%eax)

movl %eax,%edx

movl %eax,(%edx)

movl (%eax),%edx

C Analog

temp = 0x4;

*p = -147;

temp2 = temp1;

*p = temp;

*temp = p;

22

Simple Addressing Modes Simple Addressing Modes

NormalNormal^ (R)(R)^ MemMem[[RegReg[R]][R]]

 Register R specifies memory address

movl (%ecx),%eax

DisplacementDisplacement^ D(R)D(R)^ MemMem[[RegReg[R]+D][R]+D]

 Register R specifies start of memory region

 Constant displacement D specifies offset

movl 8(%ebp),%edx

25

Using Simple Addressing Modes Using Simple Addressing Modes

**void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; xp = t1; yp = t0; }

swap: pushl %ebp movl %esp,%ebp pushl %ebx

movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx)

movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

Body

Set Up

Finish

26

Understanding Swap Understanding Swap

**void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; xp = t1; yp = t0; }

**movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # xp = eax movl %ebx,(%ecx) # yp = ebx

Stack

Register Variable %ecx yp %edx xp %eax t %ebx t

yp xp Rtn adr (^0) Old % ebp %ebp

Offset

-4 Old % ebx

27

Understanding Swap Understanding Swap

**movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # xp = eax movl %ebx,(%ecx) # yp = ebx

0x 0x Rtn adr

%ebp 0

Offset

Address

0x 0x 0x11c 0x 0x 0x 0x10c 0x 0x 0x

yp xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x

28

Understanding Swap Understanding Swap

**movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # xp = eax movl %ebx,(%ecx) # yp = ebx

0x 0x Rtn adr

%ebp 0

Offset

Address

0x 0x 0x11c 0x 0x 0x 0x10c 0x 0x 0x

yp xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x

0x

31

Understanding Swap Understanding Swap

**movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # xp = eax movl %ebx,(%ecx) # yp = ebx

0x 0x Rtn adr

%ebp 0

Offset

Address

0x 0x 0x11c 0x 0x 0x 0x10c 0x 0x 0x

yp xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x

0x

123

0x

32

Understanding Swap Understanding Swap

**movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # xp = eax movl %ebx,(%ecx) # yp = ebx

0x 0x Rtn adr

%ebp 0

Offset

Address

0x 0x 0x11c 0x 0x 0x 0x10c 0x 0x 0x

yp xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x

0x

123

0x

33

Understanding Swap Understanding Swap

**movl 12(%ebp),%ecx # ecx = yp movl 8(%ebp),%edx # edx = xp movl (%ecx),%eax # eax = *yp (t1) movl (%edx),%ebx # ebx = *xp (t0) movl %eax,(%edx) # xp = eax movl %ebx,(%ecx) # yp = ebx

0x 0x Rtn adr

%ebp 0

Offset

Address

0x 0x 0x11c 0x 0x 0x 0x10c 0x 0x 0x

yp xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x

0x

123

0x

34

Indexed Addressing Modes Indexed Addressing Modes

Most General FormMost General Form

D( D(RbRb,,RiRi,S),S) (^) MemMem[[RegReg[[RbRb]+S]+SRegReg[[RiRi]+ D]]+ D]

 D: Constant “displacement” 1, 2, or 4 bytes

 Rb: Base register: Any of 8 integer registers

 Ri: Index register: Any, except for %esp

Unlikely you’d use %ebp , either

 S: Scale: 1, 2, 4, or 8

Special CasesSpecial Cases

( (RbRb,,RiRi)) MemMem[[RegReg[[RbRb]+]+RegReg[[RiRi]]]]

D( D(RbRb,,RiRi)) (^) MemMem[[RegReg[[RbRb]+]+RegReg[[RiRi]+D]]+D]

( (RbRb,,RiRi,S),S) (^) MemMem[[RegReg[[RbRb]+S]+SRegReg[[RiRi]]]]

37

0x10C0x10C 0x11 0x

0x1080x108 0x13 0x

0x1040x104 0x00 0x

0x1000x100 0xFF 0xFF

AddressAddress Value Value

%%ebxebx 0x8 0x

%%edxedx 0x1 0x

%%ecxecx 0x104 0x

%%eaxeax 0x100 0x

RegisterRegister Value Value

(% (%ecxecx,%,%ebxebx))

(% (%eaxeax, %, %edxedx, 4), 4)

254(,% 254(,%edxedx,2),2)

3(% 3(%eaxeax,%,%edxedx))

OperandOperand ValueValue



 





Exercise Exercise

38

More on Data Movement More on Data Movement

MOVB and MOVW MOVB and MOVW

MOVW moves two bytes, when oneMOVW moves two bytes, when one

of its operands is a register, itof its operands is a register, it

must be one of the 8 twomust be one of the 8 two--bytebyte

registersregisters

e.g. MOVW %ax, %e.g. MOVW %ax, %dxdx

MOVB moves a single byte, when oneMOVB moves a single byte, when one

of its operands is a register, itof its operands is a register, it

must be one of the 8 singlemust be one of the 8 single--bytebyte

registersregisters

e.g. MOVB %al, %ahe.g. MOVB %al, %ah

%esi

%edi

%esp

%ebp

%ah %al

%dh %dl

%ch %cl

%bh %bl

%eax

%edx

%ecx

%ebx

39

MOVSBL and MOVZBL MOVSBL and MOVZBL

 MOVSBL sign-extends a single byte, and copies it into a

double-word destination

 MOVZBL expands a single byte to 32 bits with 24 leading

zeros, and copies it into a double-word destination

Example: Example:

% %eaxeax == 0x123456780x

% %edxedx == 0xAAAABBBB0xAAAABBBB

MOVB MOVB %dh,%dh, %al%al %%eaxeax ==

MOVSBL MOVSBL %dh,%dh, %%eaxeax %%eaxeax ==

MOVZBL MOVZBL %dh,%dh, %%eaxeax %%eaxeax ==

More on Data Movement More on Data Movement

40

% %eaxeax (^) = 0x12345678= 0x

% %edxedx (^) = 0xAAAA22CC= 0xAAAA22CC

MOVB MOVB %dh,%dh, %ah%ah %%eaxeax ==

MOVSBL MOVSBL %dh,%dh, %%eaxeax %%eaxeax ==

MOVZBL MOVZBL %dh,%dh, %%eaxeax %%eaxeax ==

MOVSBL MOVSBL %dl,%dl, %%eaxeax %%eaxeax ==

                               

 

 

 

 

Exercise Exercise