190 likes | 357 Views
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
E N D
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 • The declaration is accomplished by giving a variable name and a type. { variablename: } .word {value}
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;
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
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
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.
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
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
.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
Procedures Four steps are required to execute a simplified procedure 1) Save the return address 2) Procedure call 3) Execute procedure 4) Return
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
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
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
## 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
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:
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
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)
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?