Transcript Document

SAL – Part 1
Simple Abstract Language
What do we need in a language?




The language must provide a way to
specify the type of the variable
It must be able to specify arithmetic
operations, e.g., add, subtract,etc.
It must provide control structures
that allow looping and conditional
execution.
It must provide a way to
communicate with a user of the
program.
Why write assembly language
programs?


There are features of the computer
that can be accessed with assembly
language that are not well-captured
in a high level language, e.g. critical
parts of an operating system
To satisfy critical constraints of a
program, e.g., it must fit in a very
small amount of memory


Unavailability of a good compiler
Compiler writers need to
understand how to write programs
in assembly language before they
can even write compilers
Variable Declaration

SAL understands three simple types:




integer
character
real
.word
.byte
.float
SAL understands one complex type:

string
.asciiz
The declaration is accomplished by
giving a variable name and a type.
{ variablename: } .word {value}

Example 2.1
C++
int temp;
int i = 5;
SAL
temp:
.word
i:
.word
5
Example 2.2
sentinel:
linefeed:
amount:
amount:
amount:
.byte ‘z’
.byte ‘\n’
.float 136.42
.float 1.2642E2
.float 10.13642e+3
Example 2.3
string:
str2:
.asciiz
.asciiz
“Hello World!”
“Goodbye Cruel World…”
Instructions and Variables in SAL

The memory is divided into two
distinct areas:



area for variables a.k.a. data space
area for instructions a.k.a. code or
text space
In SAL, declarations can occur
anywhere, but they must be
separated from code by the use of
directives or pseudo-instructions.


The code space is distinguished in
SAL by preceding it with
.text
The data space is distinguished in
SAL by preceding it with
.data
Arithmetic Operations

A SAL instruction consists of an
operation specification, known as
opcode, and two or three operand
specification.
move
add
sub
mul
div
rem
x,y
x,y,z
x,y,z
x,y,z
x,y,z
x,y,z
x
x
x
x
x
x
=
=
=
=
=
=
y;
y +
y y *
y /
y %
z;
z;
z;
z;
z;
Example 2.4
areatri = (width*height)/2;
areatri:
width:
height:
tmp:
mul
div
.float
.float
.float
.float
tmp,width,height
areatri,tmp,2.0
Example 2.5
.data
#data space begins
areatri:.float
#declare areatri as float
width: .float 10.0 #declare and initialize width
height: .float 5.0 #declare and initialize height
tmp:
.float
#declare tmp
.text
#code space begins
__start:
#begin execution -required label
mul tmp,width,height
#tmp := width * height
div areatri,tmp,2.0
#aretri := tmp/2
put areatri
#display the value of areatri
put '\n’
#put a newline character
done
#a macro to indicate end of program
Example 2.6
Write a SAL program to find the cube
of a number, say 5. Here’s the start:
.data
result: .float
base:
.float 5.0
.text
__start:
????
Control Structures

C++ provides two categories of
control structures:

conditionals


iteratives


for example, if-then statement
for example, while statement
In SAL, these control structures can
be done using a construct called
branch
SAL’s branch instructions
Listed below are some of SAL’s branch
instructions. See Table 2.2 of your
textbook for a complete list.
b
label
beq
x,y,label
ble
x,y,label
bltz x,label
bnez x,label
bgtz x,label
goto label
if x==y then goto label
if x<=y then goto label
if x<0 then goto label
if x!=0 then goto label
if x>0 then goto label
Example 2.7
C++:
if ( A > 0 )
B = C / A;
else
B = A + 10;
SAL:
else:
continue:
blez A, else
div
B,C,A
b
continue
add
B,A,10
Example 2.8
C++:
if ( A > 0 )
B = C * A;
else
B = A - 10;
SAL:
ifpart:
continue:
bgtz ___, ifpart
___
___,___,___
b
continue
___
___,___,___
Unconditional branch
An unconditional branch can also be
constructed from a special case of a
conditional branch
b next
#unconditional branch to next
beqz 0, next
#if 0=0 then goto next
Compound conditional
C++:
if ((A==B) || (C<D)) {
A = A + 1;
B = B - 1;
}
SAL:
do_if:
endif:
beq
blt
b
add
sub
A,B,do_if
C,D,do_if
endif
A,A,1
B,B,1
Example 2.9
C++:
if (((A==B) && (C==D)) || (E<0)) {
A = A + 1;
C = E;
}
SAL:
check_E:
do_if:
end_if:
bne
beq
bgez
add
move
A,B,check_E
C,D,do_if
E,end_if
A,A,1
C,E
Loop induction
Unlike HLLs, code in SAL
must contain an instruction
to increment the loop
variable.
C++:
int base = 5, exp=4, result=1;
for (int i =0; i < exp; i++)
result*=base;
result:
.word
1
counter:
.word
1
exp:
.word
4
base:
.word
5
for:
bgt counter,exp,endfor
mul result,result,base
add counter,counter,1
b for
endfor:
How to run your SAL programs



Create a text file containing your
code.
On the miller prompt, run the
simulator by typing:
spimsal
To load your program, type
load “filename”


To run your program, type
run
To exit spimsal, type
exit
Using script to record your
sessions


On miller, type: script {filename}
To display your code, type: cat
myfile



Run spimsal, then load and run
your program
Type exit, to exit script
You may be asked to submit the file
filename
A quick review in Unix







text editors - pico, emacs, vi
cp - copy file, mv - move files
redirection: >, >>, <
ftp - file transfer protocol
telnet sessions
ls - list files
pwd - check your current directory