Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

28
Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002

Transcript of Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

Page 1: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

Assembly language: arithmetic and load/store

instructions

Ellen Spertus

MCS 111

September 17, 2002

Page 2: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

2

Big picture

Java/Pascal/C code

Assembly language

Machine language

Hardware

count = index + 1

add $s1, $s2, 1

00010111 00010010 00000001

Page 3: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

3

The role of assembly language

• Every instruction in a high-level programming language (such as Java) is converted into several assembly instructions.

• A program called an assembler converts each assembly instruction to a machine instruction.

• The hardware “knows how to” run machine instructions.

Page 4: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

4

Review: hardware

• Flip-flops (aka registers)– Can remember its old input– Can remember a new input

• Multiplexers– Choose one of many inputs

• Selector– Select one of many outputs

s1

mux

sel

Page 5: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

5

Instruction execution

ALU

s1

s0

s2

assembly: add $s1 , $s2 , $s3machine: 0111 0001 0010 0011

s3

mux mux

Page 6: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

6

Addition

add $s1, $s2, $s3– Meaning: $s1 = $s2 + $s3

– Operation: add– Operands: $s1, $s2, $s3– Exactly three operands– Each operand must be a register

Page 7: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

7

Translating Java code

int a;

int b;

int c;

a = b + c;

a: $s1

b: $s2

c: $s3

Page 8: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

8

A closer look at registers

• The MIPS architecture has 32 registers

• Each register is bits wide

• Registers corresponding to Java variables are called $s0, $s1, ...

• Registers corresponding to temporary (unnamed) values are called $t0, $t1, ...

Page 9: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

9

Converting complex instructions

• a = b + c + d;

• add $t0, $s2, $s3

• add $s1, $t0, $s4

a: $s1

b: $s2

c: $s3

d: $s4

Page 10: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

10

Practice

• b = b + c + a; a: $s1

b: $s2

c: $s3

Page 11: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

11

Subtraction

sub $s1, $s2, $s3– Meaning: $s1 = $s2 - $s3

– Operation: sub– Operands: $s1, $s2, $s3– Exactly three operands– Each operand must be a register

Page 12: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

12

Practice

• a = a - (b + d); a: $s1

b: $s2

c: $s3

d: $s4

Page 13: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

13

Practice

• a = b - c - (a + d); a: $s1

b: $s2

c: $s3

d: $s4

Page 14: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

14

Translation steps

• Java code: a = b + c

• Assembly code: add $s1, $s2, $s3

• Machine code: 00000010010100111000100000100000

Page 15: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

15

The need for more memory

• Why aren’t 32 registers enough memory for a modern computer?

Page 16: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

16

Page 17: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

17

Main memory

address contents

0

1

2

3

: :

How big?

Page 18: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

18

Load/store instructions

• Purpose: Transfer data between registers and memory

• Load instructions– lb: load byte– lw: load word

• Store instructions– sb: store byte– sw: store word

Page 19: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

19

Load-byte instruction

• Transfer data from memory to a register

• Load-byte: lb– Format: lb $ra, i($rb)– Example: lb $t2, 2($t0)– Meaning: Take the value in memory location

(2+$t0) and put it in $t2

Note: i stands for immediate

Page 20: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

20

Example: lb and sb

address contents

0

1 4510

2 1010

3

: :

count

sum

sum = sum + count;

Assume $t0 = 0

lb $t2, 2($t0)

lb $t1, 1($t0)

add $t1, $t1, $t2

sb $t1, 1($t0)8 bits

Page 21: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

21

Bytes and words

address contents

0 00000000two

1 00000000two

2 00000000two

3 00000001two

: :

} one byte

one word (4 bytes)

8 bits

Page 22: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

22

How to combine bytes into word

• Little-endian– The “little end” (least significant byte) is

stored first in memory.– Used by Intel

• Big-endian– The “big end” (most significant byte) is

stored first in memory.– Used by MIPS, Motorola

Page 23: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

23

Bytes and words

address contents

0 00000000two

1 00000000two

2 00000000two

3 00000001two

: :

} one byte

one word (4 bytes)

8 bits

Page 24: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

24

Word example

address contents 0 00000000 1 00000000 2 00000000 3 00000101 4 00000000 5 00000000 6 00000000 7 00001010 : :

count

sum

sum = sum + count;

Assume $t0 = 0

lw $t2, _($t0)

lw $t1, _($t0)

add $t1, $t1, $t2

sw $t1, _($t0)

Page 25: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

25

Summary: levels of languages

• People like to program in high-level languages, such as Java.

• The hardware knows how to run low-level machine code.

• High-level code is converted into machine language by compilers, assemblers, and students.

Page 26: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

26

Summary: assembly language

• The add and sub operations act on registers.

• The load and store operations transfer data between memory and registers.

• How to order bytes within a word?– Little-endian– Big-endian– Bi-endian or “byte-sexual”

Page 27: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

27

Page 28: Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

28

“His Majesty desired I would take some other opportunity of bringing all the rest of his enemy's ships into his ports. And so unmeasurable is the ambition of princes, that he seemed to think of nothing less than reducing the whole empire of Blefuscu into a province, and governing it by a Viceroy; of destroying the Big-Endian exiles, and compelling that people to break the smaller end of their eggs, by which he would remain the sole monarch of the whole world.”

— Gulliver’s Travels by Jonathan Swift