Chapter2-bw

Chapter2-bw - Chapter 2: SAL -- A Simple Chapter -- Simple...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 2: SAL -- A Simple Chapter -- Simple Abstract Language Abstract EEC 70 Fall 2010 EEC Professor Kent Wilken Professor 1 Motivation for SAL • SAL is an intermediate-level language • Makes the transition from high-level language to level low-level (machine) language easier to understand low • Specific details of the machine language can be Specific postponed postponed • Progression of learning, with increasing detail: HLL SAL MAL TAL Machine Code 2 A Simple Abstract Language, SAL imple bstract • High level languages include various High abstract data types/data structures abstract • SAL includes only concrete data types, SAL concrete data those that are implemented in the hardware: hardware: • • • • integer character floating point Not objects, records/structures, arrays, objects, Boolean, etc. These are constructed by the compiler as a collection or subset of the concrete data types 3 Programming Language Programming Requirements Requirements • SAL meets the fundamental requirements SAL for a programming language: for • Data Declarations identify data type and identify space requirements (size) space • Arithmetic and Logical operations • Conditional Execution (elemental form of IFThen-Else) • Looping Constructs (follows from conditional (follows execution) execution) • Input/Output 4 SAL’s Top-Level Syntax • One instruction or data declaration per line • Instructions have at most two operands and Instructions produce at one result produce • Some instructions have a single operand • Comments are anything on a line that follows Comments “ #” • Multiple-line comments require “#” on each line line 5 SAL Data Declarations • Indicate how much memory space a data item Indicate needs needs • Allow a name (label) to be assigned to that Allow memory space memory • There are three different data types: There integer, floating point, and character integer, • Example: C/Java: SAL: int area; area: .word 6 Declarations (Continued) • SAL declaration syntax: variable_name: data type initial_value • types are: .word, .byte, and .float • Examples: count: yes: pi: .word 0 .byte ‘y’ .byte .float 3.14159 .float 3.14159 • Initial values are always defined • default value is 0 for .word and .float • “null” character for .byte 7 Declarations (Continued) • Format for initial .float values is: {+,-} {digits} . {digits} {e,E} {+,-} {digits} Examples that are the same value: 123.456 1.23456E2 +12.3456e1 0.123456e+3 123456E-3 • One declaration per line 8 Directives • A directive is a way of providing information to directive is the assembler the • All directives start with ‘.’ (period) All • Examples: .byte # allocate space for 1 character .word # allocate space for 1 integer .float # allocate space for 1 real .data # identifies the start of a data section in the program # there can be multiple data sections .text # identifies the start of a code section # there can be multiple code sections 9 Arithmetic Operations • SAL’s arithmetic operations are similar to arithmetic HLL arithmetic operations, except the number of operands is limited to 2 or 1 number • Syntax is different Syntax SAL C/Java move x,y add x,y,z sub x,y,z mul x,y,z div x,y,z rem x,y,z x= y; x= y+z; x= y-z; x= y* z; x= y / z; x= y % z; 10 Arithmetic Operations (cont.) • Operands can be a variable (represented Operands by a label), or a constant. Examples: by move count, 0 mul product,mult1,mult2 add sum,this,that • SAL also includes Boolean operations SAL (and, or, xor, etc.) which we’ll cover later (and, 11 Conditional Execution • Based on a program’s logic, sometimes an logic, instruction should be executed, sometimes not instruction • In C/Java: if (condition) statement else statement statement • SAL has conditional branches (“conditional conditional gotos”) General forms: gotos bcond x,y label bcond x,0 label b label # if cond(x,y) is true, then branch (goto) label # if cond(x,0) is true, then branch (goto) label if # branch unconditionally to label 12 Branch Conditions • SAL allows every kind of comparison SAL between x,y and between x,0: between beq x,y,label beq bne x,y,label bne #branch to label if x=y #branch #branch to label if x<>y #branch blt x,y,label blt bgt x,y,label bgt #branch to label if x<y #branch #branch to label if x>y #branch ble x,y,label ble bge x,y,label bge #branch to label if x<=y #branch #branch to label if x>=y #branch beqz x,label beqz bnez x,label bnez #branch to label if x=0 #branch #branch to label if x<>0 #branch bltz x,label bltz bgtz x,label bgtz #branch to label if x<0 #branch #branch to label if x>0 #branch blez x,label blez bgez x,label bgez #branch to label if x<=0 #branch #branch to label if x>=0 #branch 13 Implementing IF in SAL • Example C/Java IF: if (count < 0) count = count + 1; • SAL implementation: SAL bltz count, ifpart bltz b next ifpart: add count,count,1 next: # next instruction goes here next 14 Compiler Optimization • Compilers use many interesting techniques Compilers to produce machine code that is fast and compact. • One optimization, reversing the comparison One is effective for our if-then-else example: is bgez count,endif add count,count,1 endif: # next instruction goes here • One instruction (unconditional branch) is One eliminated, so the program is smaller and faster faster 15 IF-ELSE • Example C/Java IF-ELSE: if (A > 0) B=C+D else else B=E+F 16 IF-ELSE (cont) • One SAL implementation: One else: endif: blez blez add add b add A, else B, C, D B, endif B, E, F • Second SAL implementation: Second if: endif: bgtz bgtz add add b add A, if B, E, F B, endif B,C,D • Which is best, or is another best? 17 IF-Else (cont) • One SAL implementation: One else: endif: blez blez add add b add A, else B, C, D B, endif B, E, F A>0 • Second SAL implementation: Second if: endif: bgtz bgtz add add b add A, if B, E, F B, endif B,C,D • Which is best, or is another best? 18 IF-Else (cont) • One SAL implementation: One else: endif: blez blez add add b add A, else B, C, D B, endif B, E, F A>0 • Second SAL implementation: Second if: endif: bgtz bgtz add add b add A, if B, E, F B, endif B,C,D • Which is best, or is another best? 19 IF-Else (cont) • One SAL implementation: One else: endif: blez blez add add b add A, else B, C, D B, endif B, E, F A>0 • Second SAL implementation: Second if: endif: bgtz bgtz add add b add A, if B, E, F B, endif B,C,D • Which is best, or is another best? 20 IF-Else (cont) • One SAL implementation: One else: endif: blez blez add add b add A, else B, C, D B, endif B, E, F • Second SAL implementation: Second if: endif: bgtz bgtz add add b add A, if B, E, F B, endif B,C,D A>0 • Which is best, or is another best? 21 IF-Else (cont) • One SAL implementation: One else: endif: blez blez add add b add A, else B, C, D B, endif B, E, F • Second SAL implementation: Second if: endif: bgtz bgtz add add b add A, if B, E, F B, endif B,C,D A>0 • Which is best, or is another best? 22 IF-Else (cont) • C/Java IF: if (A > 0) B=C+D else B=E+F • Is the same as: B=E+F if (A > 0) if B=C+D • SAL: SAL: endif: add B, E, F B, bgtz A, endif add B,C,D 23 Compound Conditionals • IF statements with compound conditions can IF be implemented in SAL using multiple branches branches • Notice reversed comparison C/Java: if ((A > 0) && (B < C)) D=E+F SAL: blez A, endif bge B, C, endif add D, E, F endif: 24 Compound Conditionals • IF statements with compound conditions can IF be implemented in SAL using multiple branches branches C/Java: if ((A > 0) || (B < C)) D=E+F SAL: bgz A, if blt B, C, if b endif if: add D, E, F if: add endif: 25 Making Loops with Conditional Making Branches Branches • We can build structured loops (for, while, We repeat, etc.) using conditional branches. • test condition at the start of loop, and branch test to first instruction following loop if true true Test (bcond) false false Loop body next instr. 26 Loop Construction Example: While Loop While • C/Java while (count > 0) { a = a % count; count = count -1; count } • SAL: while: endwhile: endwhile: blez count, endwhile rem a,a,count add count,count,-1 b while # next instruction here next 27 Loop Construction Example: For Loop For •C/Java: for (i=0; i<10; i++) a = a+i; •SAL: for: endfor: move t1,10 move i,0 bge i,t1, endfor add a,a,i add i,i,1 b for # next instruction 28 SAL’s Input/Out Operations • It is necessary for a program to communicate with It the outside world the • the user, via keyboard, display, mouse, speakers, the microphone, etc. microphone, • attached peripherals, e.g., disk, printer, scanner, etc. • other networked computers • SAL has a very simple set of input/output SAL commands for keyboard and character-display I/O commands • put x # value of x is written to display • get x # x is assigned the value input on keyboard • puts string # writes a character string to display 29 SAL’s I/O Abstraction • Computer abstraction suggests keyboard and display Computer use base 10 integer and real numbers, and alphanumeric characters alphanumeric Memory chars reals integers my program strings reals integers Processor 30 I/O Reality • I/O is really binary • SAL communicates to input/output programs SAL (drivers) that directly manage I/O devices (drivers) Memory my program Processor 1101101 10110 output program 001010 010100 input program 31 String Declarations and Special String Characters Characters • Can declare a contiguous set of characters, a Can string, using SAL’s string declaration: string label: .ascii string where string is a character sequence where string is enclosed in double quote marks (“ “) enclosed prompt: .ascii “Type in your name” • Besides the usual alphabet and numeric Besides characters, SAL uses various special characters characters • null character, ‘\0’, is a character that does nothing. Is used to null is mark the end of a string, a “null-terminated” string. • newline character, ‘\n’, indicates a carriage return on input or newline indicates on output causes an advance to a new line on the display. on 32 String Declarations (cont.) • A string label is associated with the memory string location (address) of the first character location • SAL puts command calls an I/O program, passing SAL puts command it the label address it • By agreement, the I/O program knows where By string ends when it finds a null character, ‘\0’. Thus: Thus: prompt: .ascii .ascii “Type in your name\0” • For convenience .asciiz directive automatically For .asciiz directive adds null character at end of string. Thus: adds prompt: .asciiz .asciiz “Type in your name” 33 Oddities of get Oddities get • get intvar places the first integer places variable on the line into intvar, and then intvar and discards the rest of the line. Example: discards Input: 23 ax 467 -14 735 1234abc! SAL code: get int1 get int2 get int3 # int1 int1 # int2 int2 # int3 int3 23 23 -14 1234 1234 34 Example I/O Program: adds 2 Integers .data prompt: linefeed: message: int1: int1: int2: sum: sum: .asciiz .byte .asciiz .word .word .word "Enter an integer: " '\n' "The sum is " .text __start: puts prompt # get an integer from user get int1 put linefeed puts prompt # get a integer from user get int2 get put linefeed put add sum, int1, int2 # calculate the sum add puts message # print out the sum puts put sum put linefeed done # end of program 35 Procedure (Function) Calls • SAL has a very simple method for SAL procedure calls procedure • Simple => fast • No special mechanism for parameter No passing, function return values, or recursion recursion • We’ll see how these are implemented in ll Chapter 9 Chapter 36 Parts of a Procedure Call Procedure Call Instruction . . . swap(a,b) d = e + *a; . . . Return Location void swap (int* x,y) { int z z = *x; *x = *y; *y = z; return } Procedure Entry Procedure Exit 37 Parts of a Procedure Call (cont.) • Four steps in a procedure call: 1. 2. 3. 3. 4. 4. Save address of return location Execute procedure call instruction Execute procedure Return • We need an assembly-language technique for language each of the four steps each • Already have a technique for steps 2 and 3: Step 2: use an unconditional branch instruction. Procedure name is the label of the procedure’s first instruction: b swap # execution will continue at procedure entry Step 3: normal code execution. Step 38 Saving Return Address • The return address is variable, because The procedure can be called from many sites. procedure • Need an instruction that places an address to a Need variable variable • SAL includes la (load address) instruction, (load which saves the address of a label into an integer variable: integer la saved_address, rtnloc # saved_address is an # integer variable, # rtnloc is a label rtnloc 39 Returning from a Call • Branch instructions discussed so far branch Branch to a fixed address, represented by a label to • For Return, we need to branch to a location For Return we that varies. Solution: branch instruction that goes to the address contained in a variable: goes b (saved_address) • The parentheses indicate branch address is The the data at location saved_address, not the address of saved_address address 40 Single Call/Return Example .data saved_address: .word . . . .text . . . la saved_address, rtnloc1 la b swap swap rtnloc1: add d,e,a rtnloc1: add . . . swap: move z,x move x,y move y,z b (saved_address) 41 Call/Return Example, 2 Call Sites . . . rtnloc1: . . . rtnloc2: . . . swap: la saved_address, rtnloc1 la b swap swap add d,e,a la saved_address, rtnloc2 la b swap swap add r,s,t move z,x move x,y move y,z b (saved_address) 42 SAL Program Examples • Let’s translate a C program into SAL /* print the nth Fibonacci number, where n>=2 */ int n, fib1=0, fib2=1 printf(“Input the Fibonacci number you want: ”); Input ); scanf ("%d",&n); scanf for (int i=2; i<=n; i++) { int temp = fib2+fib1; fib1=fib2; fib2=temp; } printf(“The Fibonacci number is %d \n”, fib2); The 43 ...
View Full Document

Ask a homework question - tutors are online