Title: Discussion
1Schedule
2Lifes Wedges Spencer W. Kimball
- On this particular day, the lad found a
fallers wedgewide, flat, and heavy, a foot or
more long, and splayed from mighty poundings. A
fallers wedge, used to help fell a tree, is
inserted in a cut made by a saw and then struck
with a sledgehammer to widen the cut. Because
he was already late for dinner, the lad laid the
wedge between the limbs of the young walnut
tree his father had planted near the front gate.
He would take the wedge to the shed right after
dinner, or sometime when he was going that way. - He truly meant to, but he never did. The wedge
was there between the limbs, a little tight, when
he attained his manhood. It was there, now firmly
gripped, when he married and took over his
fathers farm. It was half grown over on the day
the threshing crew ate dinner under the tree.
Grown in and healed over, the wedge was still in
the tree the winter the ice storm came. - In the chill silence of that wintry night, one
of the three major limbs split away from the
trunk and crashed to the ground. This so
unbalanced the remainder of the top that it, too,
split apart and went down. When the storm was
over, not a twig of the once-proud tree remained.
3Lifes Wedges Spencer W. Kimball
- In the chill silence of that wintry night, one
of the three major limbs split away from the
trunk and crashed to the ground. This so
unbalanced the remainder of the top that it, too,
split apart and went down. When the storm was
over, not a twig of the once-proud tree remained. - Early the next morning, the farmer went out to
mourn his loss. - Then, his eyes caught sight of something in the
splintered ruin. The wedge, he muttered
reproachfully. The wedge I found in the south
pasture. A glance told him why the tree had
fallen. Growing, edge-up in the trunk, the wedge
had prevented the limb fibers from knitting
together as they should.
4Chapter 9TRAP Routines andSubroutines
5Subroutines
- Weve seen that Trap routines can be very useful
- Allows user to do I/O without worrying about the
details. - A Trap routine is a form of a subroutine.
- A subroutine is a program fragment that performs
some useful function. - General library functions such as sine,
square-root, etc. - Specific functions that only apply to one program
- Provides a way to organize the program.
- Keeps the program smaller (no need to repeat
code). - Smaller programs are easier to maintain.
- Fewer bugs copying code repeats bugs.
6Repetition
some instruction some instruction some
instruction LABEL1 LDI R3, DSR
BRzp LABEL1 STI R0, DDR some
instruction some instruction some
instruction LABEL2 LDI R3, DSR
BRzp LABEL2 STI R0, DDR some
instruction some instruction LABEL3 LDI R3,
DSR BRzp LABEL3 STI R0, DDR
LABEL LDI R3, DSR BRzp LABEL STI R0, DDR
7Subroutines reduce repetition.
some instruction some instruction some
instruction some instruction JSR Output some
instruction some instruction JSR Output some
instruction some instruction JSR Output
Output ST R3, SaveR3 Output1 LDI R3, DSR
BRzp Output1 STI R0, DDR LD R3, SaveR3 RET
8Subroutine linkage
- A subroutine is called with a JSR or JSRR
instruction. - The address of the next instruction after the
subroutine call (PC) is loaded by the processor
into register R7. - At the end of a subroutine, a RET instruction
loads register R7 into the program counter.
9JSR Instruction
- Jumps to a location (like a branch but
unconditional),and saves current PC (addr of
next instruction) in R7. - saving the return address is called linking
- target address is PC-relative (PC
Sext(IR100)) - bit 11 specifies addressing mode
- if 1, PC-relative target address PC
Sext(IR100) - if 0, register target address contents of
register IR86
10JSR
NOTE PC has already been incrementedduring
instruction fetch stage.
11JSRR Instruction
- Just like JSR, except Register addressing mode.
- target address is Base Register
- bit 11 specifies addressing mode
- What important feature does JSRR providethat JSR
does not?
12JSRR
NOTE PC has already been incrementedduring
instruction fetch stage.
13TRAP
NOTE PC has already been incrementedduring
instruction fetch stage.
14RET (JMP R7)
- How do we transfer control back to instruction
following the JSR, JSRR, or TRAP? - We saved old PC in R7.
- JMP R7 gets us back to the user program at the
right spot. - LC-3 assembly language lets us use RET
(return)in place of JMP R7. - Must make sure that service routine does not
change R7, or we wont know where to return.
15TRAP x23 (IN) with Subroutines.
.ORIG X04A0 START ST R7, SaveR7 JSR SaveReg LD
R2, NEWLINE JSR WriteChar LEA R1,
Prompt Loop LDR R2, R1, 0 BRz
Input JSR WriteChar ADD R1, R1, 1 BRnzp
Loop Input JSR ReadChar ADD R0, R2, 0
JSR WriteChar LD R2, Newline JSR WriteChar JS
R RestorReg LD R7, SaveR7 RET
SaveReg ST R1, SaveR1 ST R2, SaveR2 ST R3,
SaveR3 ST R4, SaveR4 ST R5, SaveR5 ST R6,
SaveR6 RET RestorReg LD R1, SaveR1 LD R2,
SaveR2 LD R3, SaveR3 LD R4, SaveR4 LD R5,
SaveR5 LD R6, SaveR6 RET SaveR1 .FILL x0000 Save
R2 .FILL x0000 SaveR3 .FILL x0000 SaveR4 .FILL x00
00 SaveR5 .FILL x0000 SaveR6 .FILL x0000 .END
WriteChar LDI R3, DSR BRzp WriteChar STI
R2, DDR RET ReadChar LDI R3, KBSR BRzp
ReadChar LDI R2, KBDR RET SaveR7 .FILL
x0000 DSR .FILL xFE04 DDR .FILL xFE06 KBSR .FILL
xFE00 KBDR .FILL xFE02 Newline .FILL x000A Prompt
.STRINGZ Input
Question Why not save R7 in the SaveReg
subroutine?
16LC-3 PUTS (TRAP x22) Subroutine
.ORIG x0450 ST R7, SaveR7 ST R0,
SaveR0 ST R1, SaveR1 ST R3, SaveR3 Loop LDR R1,
R0, 0 BRz Return L2 LDI R3, DSR
BRzp L2 STI R1, DDR ADD R0, R0, 1 BRnzp
Loop Return LD R7, SaveR7 LD R0, SaveR0 LD R1,
SaveR1 LD R3, SaveR3 RET
Why save and restore R7?
SaveR0 .FILL x0000 SaveR1 .FILL
x0000 SaveR3 .FILL x0000 SaveR7 .FILL
x0000 DSR .FILL xFE04 DDR .FILL xFE06 .END
17Using Library Routines
- Remember .EXTERNAL symbols from last
chapter? .EXTERNAL SQRT - ... LD R2, SQAddr load SQRT
address JSRR R2 ...SQAddr .FILL SQRT - Use JSRR when you dont know if SQRT is within
1024 instructions of current location.
18Assemble and Link
Executable Image
Object Module For A
.EXTERNAL SQRT JSRR .END
Assemble
Symbol Table
Link
Object Module For Math Lib
Symbol Table
19What about User Code?
- Service routines provide three main functions
- Shield programmers from system-specific details.
- Write frequently-used code just once.
- Protect system resources from malicious/clumsy
programmers. - Are there any reasons to provide the same
functions for non-system (user) code?
20Example Negate the value in R0
- T2sComp NOT R0, R0 flip bits ADD R0, R0,
1 add one RET return to caller - To call from a program (within 1024
instructions) - need to compute R4 R1 - R3 ADD R0, R3, 0
copy R3 to R0 JSR T2sComp negate ADD R4,
R1, R0 add to R1 ...
21Passing Information to/from Subroutines
- Arguments
- A value passed in to a subroutine is called an
argument. - This is a value needed by the subroutine to do
its job. - Examples
- In 2sComp routine, R0 is the number to be negated
- In OUT service routine, R0 is the character to be
printed. - In PUTS routine, R0 is address of string to be
printed. - Return Values
- A value passed out of a subroutine is called a
return value. - This is the value that you called the subroutine
to compute. - Examples
- In 2sComp routine, negated value is returned in
R0. - In GETC service routine, character read from the
keyboardis returned in R0.
22Using Subroutines
- In order to use a subroutine, a programmer must
know - its address (or at least a label that will be
bound to its address) - its function (what does it do?)
- NOTE The programmer does not need to knowhow
the subroutine works, butwhat changes are
visible in the machines stateafter the routine
has run. - its arguments (where to pass data in, if any)
- its return values (where to get computed data, if
any)
23Saving and Restore Registers
- Since subroutines are just like service
routines,we also need to save and restore
registers, if needed. - Generally use callee-save strategy,except for
return values. - Save anything that the subroutine will alter
internallythat shouldnt be visible when the
subroutine returns. - Its good practice to restore incoming arguments
to their original values (unless overwritten by
return value). - Remember You MUST save R7 if you call any
othersubroutine or service routine (TRAP). - Otherwise, you wont be able to return to caller.
24Example
- Write a subroutine FirstChar to
- find the first occurrence of a particular
character (in R0) in a string (pointed to by
R1) return pointer to character or to end of
string (NULL) in R2. - Use FirstChar to write CountChar, which
- counts the number of occurrences of a particular
character (in R0) in a string (pointed to by
R1)return count in R2. - Can write the second subroutine first, without
knowing the implementation of FirstChar!
25CountChar Algorithm (using FirstChar)
save regs
R1 lt- R2 1
call FirstChar
save R7,since were using JSR
R3 lt- M(R2)
restore regs
R30
no
return
yes
26CountChar Implementation
- CountChar subroutine to count occurrences of a
charCountChar ST R3, CCR3 save
registers ST R4, CCR4 ST R7, CCR7 JSR
alters R7 ST R1, CCR1 save original
string ptr AND R4, R4, 0 initialize count
to zeroCC_NEXT JSR FirstChar find next
occurrence (ptr in R2) LDR R3, R2, 0 see
if char or null BRz CC_DONE if null,
no more chars ADD R4, R4, 1 increment
count ADD R1, R2, 1 point to next char in
string BRnzp CC_NEXTCC_DONE ADD R2, R4, 0
move return val (count) to R2 LD R3, CCR3
restore regs LD R4, CCR4 LD R1,
CCR1 LD R7, CCR7 RET and return
27FirstChar Algorithm
R3R0
save regs
yes
R2 lt- R1
no
R2 lt- R2 1
R3 lt- M(R2)
restore regs
R30
no
return
yes
28FirstChar Implementation
- FirstChar subroutine to find first occurrence
of a char - FirstChar ST R3, FCR3 save registers
ST R4, FCR4 save original char NOT R4, R0
negate R0 for comparisons ADD R4, R4,
1 ADD R2, R1, 0 initialize ptr to
beginning of string - FC_NEXT LDR R3, R2, 0 read character
BRz FC_DONE if null, were done ADD R3,
R3, R4 see if matches input char
BRz FC_DONE if yes, were done ADD R2,
R2, 1 increment pointer BR FC_NEXT - FC_DONE LD R3, FCR3 restore registers
LD R4, FCR4 RET and return