1 / 27

SAL – Part 1

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.

river
Download Presentation

SAL – Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SAL – Part 1 Simple Abstract Language

  2. 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.

  3. 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

  4. Unavailability of a good compiler • Compiler writers need to understand how to write programs in assembly language before they can even write compilers

  5. 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}

  6. Example 2.1 C++ int temp; int i = 5; SAL temp: .word i: .word 5

  7. Example 2.2 sentinel: .byte ‘z’ linefeed: .byte ‘\n’ amount: .float 136.42 amount: .float 1.2642E2 amount: .float 10.13642e+3

  8. Example 2.3 string: .asciiz “Hello World!” str2: .asciiz “Goodbye Cruel World…”

  9. 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.

  10. The code space is distinguished in SAL by preceding it with .text • The data space is distinguished in SAL by preceding it with .data

  11. 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;

  12. Example 2.4 areatri = (width*height)/2; areatri: .float width : .float height: .float tmp: .float mul tmp,width,height div areatri,tmp,2.0

  13. 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

  14. 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: ????

  15. 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

  16. 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

  17. 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:

  18. Example 2.8 C++: if ( A > 0 ) B = C * A; else B = A - 10; SAL: bgtz ___, ifpart ___ ___,___,___ b continue ifpart: ___ ___,___,___ continue:

  19. 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

  20. 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:

  21. 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:

  22. 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;

  23. 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:

  24. 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”

  25. To run your program, type run • To exit spimsal, type exit

  26. 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

  27. 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

More Related