assembly language: arithmetic and load/store instructions ellen spertus mcs 111 september 17, 2002
TRANSCRIPT
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
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.
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
5
Instruction execution
ALU
s1
s0
s2
assembly: add $s1 , $s2 , $s3machine: 0111 0001 0010 0011
s3
mux mux
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
7
Translating Java code
int a;
int b;
int c;
a = b + c;
a: $s1
b: $s2
c: $s3
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, ...
9
Converting complex instructions
• a = b + c + d;
• add $t0, $s2, $s3
• add $s1, $t0, $s4
a: $s1
b: $s2
c: $s3
d: $s4
10
Practice
• b = b + c + a; a: $s1
b: $s2
c: $s3
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
12
Practice
• a = a - (b + d); a: $s1
b: $s2
c: $s3
d: $s4
13
Practice
• a = b - c - (a + d); a: $s1
b: $s2
c: $s3
d: $s4
14
Translation steps
• Java code: a = b + c
• Assembly code: add $s1, $s2, $s3
• Machine code: 00000010010100111000100000100000
15
The need for more memory
• Why aren’t 32 registers enough memory for a modern computer?
16
17
Main memory
address contents
0
1
2
3
: :
How big?
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
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
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
21
Bytes and words
address contents
0 00000000two
1 00000000two
2 00000000two
3 00000001two
: :
} one byte
one word (4 bytes)
8 bits
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
23
Bytes and words
address contents
0 00000000two
1 00000000two
2 00000000two
3 00000001two
: :
} one byte
one word (4 bytes)
8 bits
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)
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.
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”
27
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