1 / 19

SAL – Part 2

SAL – Part 2. Procedures. Program Structure. Data declarations Start declaration Code Done .data __start: .text done. Variable Declaration. SAL understands three simple types: integer .word character .byte real .float SAL understands one complex type: string .asciiz

emory
Download Presentation

SAL – Part 2

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

  2. Program Structure • Data declarations • Start declaration • Code • Done .data __start: .text done

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

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

  5. SAL’s branch instructions Listed below are some of SAL’s branch instructions. b label goto label beq x,y,label if (x==y) goto label bne x,y,label if (x!=y) goto label ble x,y,label if (x<=y) goto label bge x,y,label if (x>=y) goto label blt x,y,label if (x<y) goto label bgt x,y,label if (x>y) goto label

  6. SAL’s branch instructions bltz x,label if (x<0) goto label bgtz x,label if (x>0) goto label beqz x,label if (x==0) goto label bnez x,label if (x!=0) goto label blez x,label if (x<=0) goto label bgez x,label if (x>=0) goto label

  7. Displaying to the Screen • The put instruction displays the contents of a single variable. • put will output for types .word, .float and .byte. • To display a variable of type .asciiz, you must use the puts command. • puts outputs characters until the null symbol is reached.

  8. Reading from keyboard • The get instruction works on a line-by-line basis for variables of type integer and floating point. • It will read the first value (if the type is either integer or float) and throw away the rest • For type character, no values are thrown away

  9. Example Program • .data • str1: .asciiz "Internal Revenue Service\n" • str2: .asciiz "Input your money:" • str3: .asciiz "You had $" • str4: .asciiz "We took $" • str5: .asciiz "You now have $" • money: .float • tax: .float

  10. .text __start: puts str1 puts str2 get money puts str3 put money put '\n' mul tax,money,3.0 div tax,tax,10.0 sub money,money,tax puts str4 put tax put '\n' puts str5 put money put '\n' done Example Program

  11. Procedures Four steps are required to execute a simplified procedure 1) Save the return address 2) Procedure call 3) Execute procedure 4) Return

  12. The procedure call is really a branch instruction • The program control must be transferred to the first instruction of the procedure • After execution, control must be transferred to the instruction after the procedure call

  13. The load address instruction • SAL provides an instruction la (load address) that places an address into a variable • The variable into which the address will be saved must be of type integer saved_address: .word la saved_address, rtnaddr

  14. move vs. la label addresscontents x 3 25 y 5 7 instructionafter execution move x, y x contains 7 la x, y x contains 5

  15. ## Ben Kuehmichel, 2004 .data num: .word 10 addr: .word num2: .float 3.1415 nl: .byte '\n' str1: .asciiz "The address is " str2: .asciiz "The value contained is " .text __start: la addr, num puts str1 put addr put nl puts str2 put num put nl put nl puts str1 la addr, num2 put addr put nl puts str2 put num2 put nl done Memory contents and Addresses

  16. Remembering the return address • Suppose there are two calls to the same procedure proc1 . . . la proc1_ret, ret_addr1 b proc1 ret_addr1: . . . la proc1_ret, ret_addr2 b proc1 ret_addr2:

  17. Return Mechanism • We want to branch to the address contained within the variable proc1_ret • We will have to use b (proc1_ret) Note: the parentheses are necessary, otherwise the program will branch to a variable and not to the contents of the variable proc1_ret

  18. Example 3.1 . . . #some code here __start: la getstring_ra, rtn1 b getstring rtn1: beqz str_length, endwhile . . . #some code here getstring: move str_length, 0 . . . #some code here b (getstring_ra)

  19. Is this as good a C++ functions? • Can I do recursion? • One if one function calls another which in turn calls the first? • Why do I have to declare all these return address variables?

More Related