Download assembler

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science 101
Assembly Language
Problems with Machine Language

Uses binary - No English-like words to make it
more readable

Only numeric memory addresses - Can not name
an instruction or data location

Difficult to change - If we leave out one
instruction, all addresses from that point on will
be incorrect.

Difficult to create data - Must use internal binary
representation
Assembly Language to the
Rescue!

Symbolic operation codes: Load, Store,

Symbolic names (user defined) for memory
addresses
Jump, Compare, etc.
Load Pay

Pseudo-operations provide services such as
data generation.
Assembler

An assembler is a program that takes a
source code program written in
assembly language and converts it to a
machine language object file.

Essentially a line by line translation.
Assembly lang
Source file
Assembler
Machine lang
Object file
Labels: Giving names to locations.

We can attach a symbolic name to any
instruction or data location by beginning the line
with a label with that name.

A label consists of the name followed by a colon.

You use labels in all of the places that addresses
would occur in machine language; i.e. you no
longer use numeric addresses.
– Address fields in instructions
 Operands of computations
 Targets of branch instructions
Pseudo-operations

Pseudo-operations are not statements that
are converted to machine code. They are
used to request services of the assembler.
Performed only when program assembled.

Hypothetical Machine Pseudo-Ops:
All of the pseudo-ops begin with a period.
.begin
-- required to mark beginning
.end
-- required to mark end
.data 30
-- puts a value at the memory
location; value in decimal
assembler converts to binary
Example: Add X to Y and Store in Z
Z=X+Y
.begin
load X
add Y
store Z
halt
X: .data 33
Y: .data 40
Z: .data 0
.end
Developing Assembly Programs

First write your algorithm in pseudocode.

Then convert to assembly.

Convert computations using labeled memory
locations and the register for variables document these.

We’ll look at techniques for converting
common control flow constructs to assembly
language.
Converting Set (Assignment)
Statements

Set <variable> to <expression>

Semantics:
Compute value of the expression
Give this value to the variable

Assembly language:
Accumulate value of expression in R
Store the value in the variable’s location
Example
Set X to 2Y + Z – 5

load Y
add Y
add Z
subtract FIVE
store X
…
X:
.data 0
Y:
.data 20
Z:
.data 30
FIVE: .data 5
…

Converting If-statements
If XY then
Statements A
Statements B
Cond
T
load X
compare Y
jumpgt BSpot
…
Translation of A
...
A Code
F
B Code
BSpot: …
Translation of B
If-statement example
.begin
User inputs number.
If number is greater
than 5 we output
the number.
 Pseudocode:
Get N
If N>5 then
Print N
Stop

DONE:
N:
FIVE:
.end
in N
load N
compare FIVE
jumpgt DONE
jumpeq DONE
out N
halt
.data 0
.data 5
Converting If-else-statements
If XY then
Statements A
else
Statements B
Statements C
load X
compare Y
jumpgt Else
…
Translation of A
F
BCode
Cond T
A Code
...
jump CSpot
Else: …
Translation of B
…
…
C Code
CSpot: Translation of C
Converting While-statements
While XY Do
Statements A
Statements B
Cond
T
WHILE:
load X
compare Y
jumpgt BSpot
…
Translation of A
...
jump WHILE
F
A Code
BSpot: …
Translation of B
B Code
…
While-statement example


Program outputs sum of 10
numbers entered by the
user.
Pseudocode:
Set Ct to 10
Set Sum to 0
While Ct>0
Get Num
Set Sum to Sum+Num
Set Ct to Ct-1
Print Sum
Stop
.begin
WHILE:
PRNT:
load TEN
store CT
clear SUM
load CT
compare ZERO
jumpeq PRNT
in NUM
load NUM
add SUM
store SUM
decrement CT
jump WHILE
out SUM
halt
While-statement example
(cont.)
.begin
WHILE:
PRNT:
load TEN
store CT
clear Sum
load CT
compare ZERO
jumpeq PRNT
in NUM
load NUM
add SUM
store SUM
decrement CT
jump WHILE
out SUM
halt
TEN:
CT:
SUM:
NUM:
ZERO:
.end
.data 10
.data 0
.data 0
.data 0
.data 0