Introduction to Assembly Language: arithmetic, load, store

Download Report

Transcript Introduction to Assembly Language: arithmetic, load, store

Assembly language:
arithmetic and load/store
instructions
Ellen Spertus
MCS 111
September 17, 2002
Big picture
Java/Pascal/C code
count = index + 1
Assembly language
add $s1, $s2, 1
Machine language
00010111 00010010 00000001
Hardware
2
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.
3
Review: hardware
• Flip-flops (aka registers)
s1
– Can remember its old input
– Can remember a new input
• Multiplexers
– Choose one of many inputs
mux
• Selector
– Select one of many outputs
sel
4
Instruction execution
s3
s2
s1
assembly: add $s1, $s2, $s3
machine: 0111 0001 0010 0011
s0
mux
mux
ALU
5
Addition
add $s1, $s2, $s3
– Meaning: $s1 = $s2 + $s3
– Operation: add
– Operands: $s1, $s2, $s3
– Exactly three operands
– Each operand must be a register
6
Translating Java code
int a;
int b;
int c;
a: $s1
b: $s2
c: $s3
a = b + c;
7
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, ...
8
Converting complex
instructions
• a = b + c + d;
• add $t0, $s2, $s3
• add $s1, $t0, $s4
a: $s1
b: $s2
c: $s3
d: $s4
9
Practice
• b = b + c + a;
a: $s1
b: $s2
c: $s3
10
Subtraction
sub $s1, $s2, $s3
– Meaning: $s1 = $s2 - $s3
– Operation: sub
– Operands: $s1, $s2, $s3
– Exactly three operands
– Each operand must be a register
11
Practice
• a = a - (b + d);
a: $s1
b: $s2
c: $s3
d: $s4
12
Practice
• a = b - c - (a + d);
a: $s1
b: $s2
c: $s3
d: $s4
13
Translation steps
• Java code: a = b + c
• Assembly code: add $s1, $s2, $s3
• Machine code:
00000010010100111000100000100000
14
The need for more memory
• Why aren’t 32 registers enough memory
for a modern computer?
15
16
Main memory
How big?
address
contents
0
1
2
3
:
:
17
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
18
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
19
Example: lb and sb
address contents
sum = sum + count;
0
sum
1
count 2
4510
1010
lb $t2, 2($t0)
lb $t1, 1($t0)
3
:
Assume $t0 = 0
:
8 bits
add $t1, $t1, $t2
sb $t1, 1($t0)
20
Bytes and words
address
contents
0
00000000two
1
00000000two
2
00000000two
3
00000001two
:
:
} one byte
one word (4 bytes)
8 bits
21
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
22
Bytes and words
address
contents
0
00000000two
1
00000000two
2
00000000two
3
00000001two
:
:
} one byte
one word (4 bytes)
8 bits
23
Word example
address
0
1
count
2
3
4
5
sum
6
7
:
contents
00000000
00000000
00000000
00000101
00000000
00000000
00000000
00001010
:
sum = sum + count;
Assume $t0 = 0
lw $t2, _($t0)
lw $t1, _($t0)
add $t1, $t1, $t2
sw $t1, _($t0)
24
Summary: levels of languages
• People like to program in high-level
languages, such as Java.
• The hardware knows how to run lowlevel machine code.
• High-level code is converted into
machine language by compilers,
assemblers, and students.
25
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”
26
27
“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
28