1 SAL ENGG COLLGE ANDTECHNOLOGY AHMEDABAD Fluid Dynamics BY 130670106066.
SAL – Part 1
description
Transcript of SAL – Part 1
![Page 1: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/1.jpg)
SAL – Part 1
Simple Abstract Language
![Page 2: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/2.jpg)
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.
![Page 3: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/3.jpg)
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
![Page 4: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/4.jpg)
Unavailability of a good compiler Compiler writers need to
understand how to write programs in assembly language before they can even write compilers
![Page 5: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/5.jpg)
Variable Declaration SAL understands three simple types:
integer .word character .byte real .float
SAL understands one complex type: string .asciiz
The declaration is accomplished by giving a variable name and a type.
{ variablename: } .word {value}
![Page 6: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/6.jpg)
Example 2.1
C++int temp;
int i = 5;
SALtemp: .word
i: .word 5
![Page 7: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/7.jpg)
Example 2.2
sentinel: .byte ‘z’
linefeed: .byte ‘\n’
amount: .float 136.42
amount: .float 1.2642E2
amount: .float10.13642e+3
![Page 8: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/8.jpg)
Example 2.3
string: .asciiz “Hello World!”str2: .asciiz “Goodbye Cruel
World…”
![Page 9: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/9.jpg)
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.
![Page 10: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/10.jpg)
The code space is distinguished in SAL by preceding it with
.text
The data space is distinguished in SAL by preceding it with
.data
![Page 11: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/11.jpg)
Arithmetic Operations
A SAL instruction consists of an operation specification, known as opcode, and two or three operand specification.
move x,y x = y;
add x,y,z x = y + z;
sub x,y,z x = y - z;
mul x,y,z x = y * z;
div x,y,z x = y / z;
rem x,y,z x = y % z;
![Page 12: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/12.jpg)
Example 2.4
areatri = (width*height)/2;
areatri: .float
width : .float
height: .float
tmp: .float
mul tmp,width,height
div areatri,tmp,2.0
![Page 13: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/13.jpg)
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
![Page 14: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/14.jpg)
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:
????
![Page 15: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/15.jpg)
Control Structures
C++ provides two categories of control structures: conditionals
for example, if-then statement iteratives
for example, while statement
In SAL, these control structures can be done using a construct called branch
![Page 16: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/16.jpg)
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 goto label
beq x,y,label if x==y then goto label
ble x,y,label if x<=y then goto label
bltz x,label if x<0 then goto label
bnez x,label if x!=0 then goto label
bgtz x,label if x>0 then goto label
![Page 17: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/17.jpg)
Example 2.7
C++: if ( A > 0 )
B = C / A;
else
B = A + 10;
SAL: blez A, else
div B,C,A
b continue
else: add B,A,10
continue:
![Page 18: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/18.jpg)
Example 2.8
C++: if ( A > 0 )
B = C * A;
else
B = A - 10;
SAL: bgtz ___, ifpart
___ ___,___,___
b continue
ifpart: ___ ___,___,___
continue:
![Page 19: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/19.jpg)
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
![Page 20: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/20.jpg)
Compound conditionalC++: if ((A==B) || (C<D)) {
A = A + 1;B = B - 1;}
SAL:beq A,B,do_ifblt C,D,do_ifb endif
do_if: add A,A,1sub B,B,1
endif:
![Page 21: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/21.jpg)
Example 2.9
C++: if (((A==B) && (C==D)) || (E<0)) {
A = A + 1;
C = E;
}
SAL:bne A,B,check_E
beq C,D,do_if
check_E: bgez E,end_if
do_if: add A,A,1
move C,E
end_if:
![Page 22: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/22.jpg)
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;
![Page 23: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/23.jpg)
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:
![Page 24: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/24.jpg)
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”
![Page 25: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/25.jpg)
To run your program, typerun
To exit spimsal, typeexit
![Page 26: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/26.jpg)
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
![Page 27: SAL – Part 1](https://reader035.fdocuments.us/reader035/viewer/2022081503/56815505550346895dc2ef24/html5/thumbnails/27.jpg)
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