lecture06 assembly language

25
COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE

Upload: programming-passion

Post on 11-Jul-2015

81 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Lecture06 assembly language

COMPUTER ORGANIZATION

AND ASSEMBLY LANGUAGE

Page 2: Lecture06 assembly language

What you need to know

So far –

Should be able to write assembly programs for

Arithmetic operations

Given memory state, and some load store operations, you

should be able to tell what is the final state of the memory

Convert assembly instructions to binary forms

R and I format instructions

Today –

Should be able to write assembly programs for

Control flow - Branches, loops

Except function calls

Page 3: Lecture06 assembly language

MIPS R3000 ISA

Instruction Categories

Arithmetic

Load/Store

Jump and Branch

Floating Point

coprocessor

Memory Management

Special

R0 - R31

PC

HI

LO

OP

OP

OP

rs rt rd sa funct

rs rt immediate

jump target

Registers

R Format

I Format

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

• 3 Instruction Formats: all 32 bits wide

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

Page 4: Lecture06 assembly language

Which end do you break your

egg? Sharp-end (Little-endian)

Rounded-end (Big-endian)

Suppose, we have these two values in

registers

We store these values in memory

1111 1111 0000 0000 1111 0000 0000 1111

0000 0000 1111 1111 0000 1111 1111 0000

$s1 =

$s2 =

lw $s1 4($t0)

lw $s2 8($t0)

$t0= 0x4080

Page 5: Lecture06 assembly language

What does the memory look like?5

1111 1111

0000 0000

0

1

Incre

asin

g A

ddre

ss

4084

4085

4086

4087

1111 0000

0000 1111

$s1 =

$s2 =

1111 1111

0000 00004088

4089

408a

408b 1111 0000

0000 1111

.

.

.

1111 1111

0000 0000

0

1

Incre

asin

g A

ddre

ss

4084

4085

4086

4087

1111 0000

0000 1111

1111 1111

0000 0000

4088

4089

408a

408b

1111 0000

0000 1111

.

.

.

$t0= 0x4080lw $s1 4($t0)lw $s2 8($t0)

1111 1111 0000 0000 1111 0000 0000 1111

1111 11110000 0000 1111 00000000 1111

Page 6: Lecture06 assembly language

Memory Addressing6

What is the word at address 4080?

Memory

Address

4080

4084

4088

1111 1111 0000 0000 1111 0000 0000 1111

1111 0000 0000 1111 1111 1111 0000 0000

0000 0000 1111 0000 0000 1111 1111 1111

1111 1111 0000 0000 1111 0000 0000 1111

• What is the byte at address 4086?

Memory

Address

4080

4084

4088

1111 1111 0000 0000 1111 0000 0000 1111

1111 0000 0000 1111 1111 1111 0000 0000

0000 0000 1111 0000 0000 1111 1111 1111

0123

4567

891011

3210

7654

111098

Page 7: Lecture06 assembly language

Memory Addressing7

What is the word at address

4080?

Little Endian Memory

Address

4080

4084

4088

1111 1111 0000 0000 1111 0000 0000 1111

1111 0000 0000 1111 1111 1111 0000 0000

0000 0000 1111 0000 0000 1111 1111 1111

1111 1111 0000 0000 1111 0000 0000 1111

• What is the byte at address 4086?

Big Endian Memory

Address

4080

4084

4088

1111 1111 0000 0000 1111 0000 0000 1111

1111 0000 0000 1111 1111 1111 0000 0000

0000 0000 1111 0000 0000 1111 1111 1111

0123

4567

891011

3210

7654

111098

1111 1111 0000 1111

Leftmost byte is word address

e.g., Motorola 68k, MIPS, Sparc, HP PA

Rightmost byte is word address

e.g., Intel 80x86, DEC Vax, DEC Alpha

Page 8: Lecture06 assembly language

Endian-ness and Alignment8

Big Endian: leftmost byte is at word address

Little Endian: rightmost byte is at word address

msb lsb

3 2 1 0

little endian byte 0

0 1 2 3

big endian byte 0

Alignment restriction: requires that objects fall on address that is multiple of their size.

Aligned

Not

Aligned

0 1 2 3

Page 9: Lecture06 assembly language

Loading and Storing Bytes

11/25/2014

9

MIPS provides special instructions to move bytes

lb $t0, 1($s3) #load byte from memory

sb $t0, 6($s3) #store byte to memory

What 8 bits get loaded and stored?

load byte places the byte from memory in the rightmost 8 bits of

the destination register

what happens to the other bits in the register?

store byte takes the byte from the rightmost 8 bits of a register and

writes it to a byte in memory

op rs rt 16 bit number

Page 10: Lecture06 assembly language

Example of Loading and Storing

Bytes10

Given following code sequence and memory state (contents are

given in hexadecimal), what is the state of the memory after

executing the code?

add $s3, $zero, $zero

lb $t0, 1($s3)

sb $t0, 6($s3)

Memory

0 0 9 0 1 2 A 0

Data Word Address (Decimal)

0

4

8

12

16

20

24

F F F F F F F F

0 1 0 0 0 4 0 2

1 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 What value is left in $t0?

What if the machine was little Endian?

$t0 = 0x00000090

$t0 = 0x00000012

Page 11: Lecture06 assembly language

Instructions for Making Decisions

Decision making instructions

alter the control flow

i.e., change the "next" instruction to be executed

MIPS conditional branch instructions:

bne $s0, $s1, Label #go to Label if $s0$s1

beq $s0, $s1, Label #go to Label if $s0=$s1

Example: if (i==j) h = i + j;

bne $s0, $s1, Lab1

add $s3, $s0, $s1

Lab1: ...

11

Page 12: Lecture06 assembly language

op rs rt 16 bit number

Assembling Branches

Instructions:bne $s0, $s1, Label #go to Label if $s0$s1 beq $s0, $s1, Label #go to Label if $s0=$s1

Machine Formats:

I format

5 16 17 ????

4 16 17 ????

• How is the branch destination address specified?

12

Page 13: Lecture06 assembly language

bne $s0,$s1,Lab1

add $s3,$s0,$s1

Specifying Branch Destinations

Could use a register (like lw and sw)

and add to it the 16-bit offset

which register?

Instruction Address Register

PC = program counter

its use is automatically implied by

instruction

PC gets updated (PC+4) during the fetch

cycle so that it holds the address of the

next instruction

limits the branch distance to -215 to +215-

1 instructions from the (instruction after

the) branch instruction, but

most branches are local anyway (principle

of locality)

...Lab1:

PC

• Could specify the memory address– But that would require 32 bit field

13

Page 14: Lecture06 assembly language

Disassembling Branch Destinations

The contents of the updated PC (PC+4) is added to the low order 16 bits of the branch instruction which is converted into a 32 bit value by

concatenated two low-order zeros to create an 18 bit number

sign-extending those 18 bits

The result is written into the PC if the branch condition is true prior to the next Fetch cycle

PCAdd

32

32 32

32

32

offset

16

32

00

sign-extend

from the low order 16 bits of the branch instruction

branch dst

address

?Add

4 32

14

Page 15: Lecture06 assembly language

op rs rt 16 bit offset

5 16 17

Assembling Branches Example

Assembly codebne $s0, $s1, Lab1

add $s3, $s0, $s1

Lab1: ...

Machine Format of bne:

I format

1

• Remember– After the bne instruction is fetched, the PC is updated to

address the add instruction (PC = PC + 4).

– Two low-order zeros are concatenated to the offset number and that value sign-extended is added to the (updated) PC

15

Page 16: Lecture06 assembly language

Another Instruction for Changing Flow

MIPS also has an unconditional branch instruction or jump instruction:

j label #go to label

Example: if (i!=j) h=i+j;

elseh=i-j;

beq $s0, $s1, Lab1add $s3, $s0, $s1j Lab2

Lab1: sub $s3, $s0, $s1Lab2: ...

16

Page 17: Lecture06 assembly language

op 26-bit address

Assembling Jumps

Instruction:j label #go to label

Machine Format:

How is the jump destination address specified?

As an absolute address formed by

concatenating the upper 4 bits of the current PC (now PC+4) to

the 26-bit address and

concatenating 00 as the 2 low-order bits

J format

2 ????

17

Page 18: Lecture06 assembly language

Disassembling Jump Destinations

The low order 26 bits of the jump instruction is converted into a 32 bit jump instruction destination address by

concatenating two low-order zeros to create an 28 bit (word) address

concatenating the upper 4 bits of the current PC (now PC+4)

to create a 32 bit instruction address that is placed into the PC prior to the next Fetch cycle

PC

4

32

26

32

00

from the low order 26 bits of the jump instruction

18

Page 19: Lecture06 assembly language

Compiling While Loops

Compile the assembly code for the C while loop where i is in $s0, j is in $s1, and k is in

$s2

while (i!=k)

i=i+j;

Loop: beq $s0, $s2, Exitadd $s0, $s0, $s1j Loop

Exit: . . .

19

Page 20: Lecture06 assembly language

op rs rt rd funct

0 16 17 8 0 42 = 0x2a

More Instructions for Making Decisions

We have beq, bne, but what about branch-if-less-than?

New instruction:

slt $t0, $s0, $s1 # if $s0 < $s1

# then

# $t0 = 1

# else

# $t0 = 0

Machine format:

20

Page 21: Lecture06 assembly language

Other Branch Instructions

Can use slt, beq, bne, and the fixed value of 0 in register $zero

to create all relative conditions

less than blt $s1, $s2, Label

less than or equal to ble $s1, $s2, Label

greater than bgt $s1, $s2, Label

great than or equal to bge $s1, $s2, Label

• Example -slt $t0, $s1, $s2 #$t0 set to 1 if

bne $t0, $zero, Label # $s1 < $s2

21

Page 22: Lecture06 assembly language

op rs funct

0 9 0 0 0 8 = 0x08

Another Instruction for Changing Flow

Most higher level languages have case or switchstatements allowing the code to select one of many alternatives depending on a single value.

Instruction: jr $t1 #go to address in $t1

Machine format:

22

Page 23: Lecture06 assembly language

Compiling a Case (Switch) Statement

switch (k) { case 0: h=i+j; break; /*k=0*/case 1: h=i+h; break; /*k=1*/case 2: h=i-j; break; /*k=2*/

Assuming three sequential words in memory starting at the address in $t4 have the addresses of the labels L0,

L1, and L2 and k is in $s2

add $t1, $s2, $s2 #$t1 = 2*k

add $t1, $t1, $t1 #$t1 = 4*k

add $t1, $t1, $t4 #$t1 = addr of JT[k]

lw $t0, 0($t1) #$t0 = JT[k]

jr $t0 #jump based on $t0

L0: add $s3, $s0, $s1 #k=0 so h=i+j

j Exit

L1: add $s3, $s0, $s3 #k=1 so h=i+h

j Exit

L2: sub $s3, $s0, $s1 #k=2 so h=i-j

Exit: . . .

$t4 L0

L1

L2

Memory

23

Page 24: Lecture06 assembly language

MIPS Instructions, so far

Category Instr Op Code Example Meaning

Arithmetic

(R format)

add 0 and 32 add $s1, $s2, $s3 $s1 = $s2 + $s3

subtract 0 and 34 sub $s1, $s2, $s3 $s1 = $s2 - $s3

Data

transfer

(I format)

load word 35 lw $s1, 100($s2) $s1 = Memory($s2+100)

store word 43 sw $s1, 100($s2) Memory($s2+100) = $s1

load byte 32 lb $s1, 101($s2) $s1 = Memory($s2+101)

store byte 40 sb $s1, 101($s2) Memory($s2+101) = $s1

Cond. Branch

br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L

br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L

set on less than 0 and 42 slt $s1, $s2, $s3 if ($s2<$s3) $s1=1 else $s1=0

Uncond. Jump

jump 2 j 2500 go to 10000

jump register 0 and 8 jr $t1 go to $t1

24

Page 25: Lecture06 assembly language

MIPS R3000 ISA

Instruction Categories

Arithmetic

Load/Store

Jump and Branch

Floating Point

coprocessor

Memory Management

Special

R0 - R31

PC

HI

LO

OP

OP

OP

rs rt rd sa funct

rs rt immediate

26-bit jump target

Registers

R Format

I Format

6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

• 3 Instruction Formats: all 32 bits wide

6 bits 5 bits 5 bits 16 bits

J Format6 bits 26 bits

25