
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to instruction set architecture (isa) using the simple abstract language (sal) as an example. The motivation for using sal, its programming language requirements, data declarations, directives, conditional execution, and input/output operations. It also includes examples of if-else statements, compound conditionals, and loop constructions.
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















Instructor: Chen‐Nee Chuah 3125 Kemper Hall Lecture: M/W/F 11am‐12noon Office Hours: (M) 12:30‐1:30pm, (W) 10 ‐11am ***** Teaching Assistants: Victor Yip, Kurtis Kredo
Variable Declaration Arithmetic Operations Control Structures Communication with users, I/O Procedures Sample Program
Chuah, Fall 2007 2
Review Questions
What are the basic components of a computer? What is a program counter? What are the 6 steps involved in the instruction execution sequence? How do you implement branches? How many operands can an assembly language instruction (or statement) have? What is a typical syntax of MIPS instructions? What is instruction set architecture?
Chuah, Fall 2007 3
SAL:SAL: Simple AbstractSimple Abstract LanguageLanguage (Chapter 2)(Chapter 2)
MAL:MAL: MIPS AssemblyMIPS Assembly LanguageLanguage (Chapter 8)(Chapter 8)
TAL:TAL: True AssemblyTrue Assembly LanguageLanguage (Chapter 10)(Chapter 10)
HLL Compiler^
Assembly Language Assembler^
Machine Code
Focus on this class
Chuah, Fall 2007 4
Motivation for SAL
SAL is an intermediate‐level language, makes the transition from high‐level language to low‐level (machine) language easier to understand ‐ Fairly easy to understand (like HLL) and straightforward to translate into MAL (or TAL) ‐ Specific details of the machine language can be postponed
Progression of learning (with increasing detail):
HLLHLL^ SALSAL^ MALMAL^ TALTAL Machine CodeMachine Code
Chuah, Fall 2007 7
SAL’s Top‐Level Syntax
One instruction or data declaration per line
Instructions have at most two operands and produce one result ‐ Some instructions have a single operand
Comments are anything on a line that follows “#”. Multiple‐line comments require “#” on each line
Chuah, Fall 2007 8
SAL Data Declarations
Indicate how much memory space a data item needs
Allow a name (label) to be assigned to that memory space
Can declare 3 different data types: integer, real, and character
Example:
C/Java: C/Java: intint area;area; SAL:SAL: area:area: .word.word
Chuah, Fall 2007 9
Declarations (Continued)
SAL declaration syntax: variable_name: type initial_value
types are: .byte, .word, and .float
Examples: count: .word 0 yes: .byte ‘y’ pi: .float 3.
Initial values are always defined ‐ default value is 0 for .word and .float ‐ “null” character for .byte
Question: So, how many bits really are there in a byte? word? float?
Chuah, Fall 2007 10
Declarations (Continued)
Format for initial .float values is: {+,‐} {digits}. {digits} {e,E} {+,‐} {digits}
Examples that are the same value:
1.23456E +12.3456e 0.123456e+ 123456E‐ 3
One declaration per line
Chuah, Fall 2007 13
MACRO: indicate program has completed, Allowing next program to run
Example
.data avg .word i1: .word 20 i2: .word 13 i3: .word 82 .text _start: add avg, i1, i add avg, avg, i div avg, avg, 3 done
Chuah, Fall 2007 14
SAL SAL C/JavaC/Java
move x,ymove x,y x= y;x= y; add x,y,zadd x,y,z x= y+z;x= y+z; sub x,y,zsub x,y,z x= y-x= y-z;z; mul x,y,zmul x,y,z x= y z;x= y z; div x,y,zdiv x,y,z x= y / z;x= y / z; rem x,y,zrem x,y,z x= y % z;x= y % z;
Arithmetic Operations
SAL’s arithmetic operations are similar to HLL arithmetic operations, except the number of operands is limited to 2 or 1 ‐ Syntax is different
Chuah, Fall 2007 15
Arithmetic Operations (cont.)
Operands can be a variable (represented by a label), or a constant. Examples: move count, 0 mul product,mult1,mult add sum,this,that SAL also includes Boolean operations (and, or, xor, etc.) which we’ll cover later
Chuah, Fall 2007 16
Conditional Execution
Sometimes an instruction should be executed, sometimes not In C/Java: if (condition) statement else statement SAL has conditional branches (“conditional gotos”) General forms: b cond x,y label # if cond (x,y) is true, then branch (goto) label b cond x,0 label # if cond (x,0) is true, then branch (goto) label b label # branch unconditionally to label
Chuah, Fall 2007 19
Compiler Optimization
Compilers use many interesting methods for producing assembly code that is fast and compact. One optimization, reversing the comparison, can be effective for the if‐then‐else example: bgez count,endif add count,count, endif: # next instruction goes here
One instruction (unconditional branch) is eliminated
Chuah, Fall 2007 20
IF‐Else
Example C/Java IF: if (A > 0) B = C + D else B = E + F
Chuah, Fall 2007 21
One SAL implementation: blez A, else add B, C, D b endif else: add B, E, F endif: Second SAL implementation: bgtz A, if add B, E, F b endif if: add B,C,D endif: Which is best, or is another best?
Chuah, Fall 2007 22
One SAL implementation: blez A, else add B, C, D b endif else: add B, E, F endif: Second SAL implementation: bgtz A, if add B, E, F b endif if: add B,C,D endif: Which is best, or is another best?
Chuah, Fall 2007 25
One SAL implementation: blez A, else add B, C, D b endif else: add B, E, F endif: Second SAL implementation: bgtz A, if add B, E, F b endif if: add B,C,D endif: Which is best, or is another best?
Chuah, Fall 2007 26
One SAL implementation: blez A, else add B, C, D b endif else: add B, E, F endif: Second SAL implementation: bgtz A, if add B, E, F b endif if: add B,C,D endif: Which is best, or is another best?
Chuah, Fall 2007 27
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) B = C + D SAL: add B, E, F blez A, endif add B,C,D endif: Reduce one line of code!
Chuah, Fall 2007 28
Breakpoint
Chuah, Fall 2007 31
C/Java while (count > 0) { a = a % count; count := count -1; }
SAL: while: blez count, endwhile rem a,a,count add count,count,- b while endwhile: # next instruction here
Chuah, Fall 2007 32
Loop Construction Example: For
C/Java: for (i=0; i<10; i++) a = a+i;
move x,10move x, move i,0move i, for:for:^ bge i,x, endforbge i,x, endfor add a,a,iadd a,a,i add i,i,1add i,i, b forb for endfor:endfor: # next instruction goes here# next instruction goes here
Chuah, Fall 2007 33
SAL’s Input Output Operations
It is necessary for a program to communicate with the outside world ‐ the user, via keyboard and display ‐ attached peripherals, e.g., printer, scanner, microphone, speaker, etc. ‐ other networked computers SAL has a very simple set of input/output commands for keyboard and character display I/O ‐ 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
Chuah, Fall 2007 34
SAL’s I/O Abstraction
my program
Processor
Memory strings reals integers
chars reals integers
Chuah, Fall 2007 37
String Declarations (cont.)
String label indicates memory location of first character SAL puts command calls an I/O program, passing it the label address By agreement, I/O program knows where string ends when it finds a null character, ‘\0’. Thus: prompt:.ascii “Type in your name\0” For convenience .asciiz directive automatically adds null character at end of string. Thus: prompt:.asciiz “Type in your name”
Chuah, Fall 2007 38
Oddities of get
get intvar places the first integer variable on the line into intvar, and then discards the rest of the line. Example:
Input: 23 ax 467 ‐ 14 735 1234abc!
SAL code: get int1 # int1 I 23 get int2 # int2 I ‐ 14 get int3 # int3 I 1234
Chuah, Fall 2007 39
.data prompt: .asciiz "Enter an integer: " linefeed: .byte '\n' message: .asciiz "The sum is " int1: .word int2: .word sum: .word .text __start: puts prompt # get an integer from user get int put linefeed puts prompt # get a integer from user get int put linefeed add sum, int1, int2 # calculate the sum puts message # print out the sum put sum put linefeed done
Chuah, Fall 2007 40
Example
What happens when you use .ascii instead of .asciiz and forget to put “\0” at the end of the string declaration