270 likes | 440 Views
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.
E N D
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 .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}
Example 2.1 C++ int temp; int i = 5; SAL temp: .word i: .word 5
Example 2.2 sentinel: .byte ‘z’ linefeed: .byte ‘\n’ amount: .float 136.42 amount: .float 1.2642E2 amount: .float 10.13642e+3
Example 2.3 string: .asciiz “Hello World!” str2: .asciiz “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 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;
Example 2.4 areatri = (width*height)/2; areatri: .float width : .float height: .float tmp: .float mul tmp,width,height div 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 • for example, if-then statement • iteratives • 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 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
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:
Example 2.8 C++: if ( A > 0 ) B = C * A; else B = A - 10; SAL: bgtz ___, ifpart ___ ___,___,___ b continue ifpart: ___ ___,___,___ 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: beq A,B,do_if blt C,D,do_if b endif do_if: add A,A,1 sub B,B,1 endif:
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:
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