Sei sulla pagina 1di 30

Structured Programming: Ch 9.1-9.

2
software
Problems
Algorithms
Prog. Lang & Interfaces
H/w – s/w interface Instruction Set Architecture
Microarchitecture (Organization)
hardware

Circuits
Devices (Transistors)
Bits

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 1


What is structured programming?

Real programs have many lines of code


• e.g., Microsoft Word is million lines
• Also many people collaborate to write one big program
Often same work needs to be done multiple times at different
points in the code on different data
• If we repeat the same code, that adds to programming
burden
• If we copy the same code, that adds to debugging burden

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 2


What is structured programming?

How to control complexity of big programs and avoid


repeating/copying code?
• Controling complexity is a key intellectual challenge in
computer engineering

By Divide and Conquer strategy of structured programming

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 3


What is structured programming?

Big code: Break it up into smaller, manageable MEANINGFUL


pieces
• Meaningful pieces do some sub-functionality
• Pieces are not random chunks of code with no meaningful
sub-functionality
• e.g,, a piece could be same as a block in your flowchart
Such pieces are called routines or subroutines or functions
Avoid repeating/copying code by making such a code a routine
Routines mesh well with our previous idea of successive
decomposition or successive refinement in structured
programming (chapter 6)

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 4


What is structured programming?

How are routines invoked? Example:


Assume you need to do the work of routine “f1”in two places
• Think of f1 as a name or label
..
call f1
..
call f1
..
somewhere else in the code, f1 routine is defined
f1: ..
..
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 5
What is structured programming?

..
call f1
..
Call 1
call f1
Call 2
..

f1: ... Return 2


.. Return 1
..
return

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 6


What is structured programming?
Call instructions are called JSR and JSRR
• Chapter 5 slides 24-25
• JSR (PC-relative) and JSRR(register+offset) are like jump
but also save the incremented PC in R7
• This incremented PC is called return address
Return called RET (returning back is called linkage)
• Chapter 5 slides 24-25
• RET returns by using whatever is in R7
• Program makes sure R7 holds correct return address
• RET is a pseudo instruction – the assembler converts it to
“JMP R7”machine instruction

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 7


What is structured programming?

Helps in programming: When thinking at program level, need to


think about WHAT each routine should do and not details of
HOW it would do it
• No need to know about KBDR if all you want to do is read
an input character
• Key idea of encapsulation – abstracting away details at
program level
• Separation of concerns
•A each routine, you can think about details of HOW

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 8


What is structured programming?

Helps in debugging: while coding you focus only on the current


routine being written
• Reduces amount of details to remember at a time -so
reduces chances of bugs
• Each routine can be tested separately – focuses and
reduces amount of details to test

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 9


Basic Polling Terminal Routine and Invocation

TERMINAL LDI R0, KBSR


BRzp TERMINAL
LDI R0, KBDR
OUTPOLL LDI R1, CRTSR
BRzp OUTPOLL
STI R0, CRTDR
RET
...
...
JSR TERMINAL

Does the above code work?

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 10


Basic Polling Terminal Routine and Invocation

TERMINAL LDI R0, KBSR


BRzp TERMINAL
LDI R0, KBDR
OUTPOLL LDI R1, CRTSR
BRzp OUTPOLL
STI R0, CRTDR
RET
...
...
JSR TERMINAL

Does the above code work?


•No, it clobbers R0 and R1 from original program

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 11


Saving aside program registers

Must save if routine will clobber a value that program may need
How to save?
• Store the needed registers in memory before the routine
starts
– Remember that JSR/JSRR/TRAP will overwrite R7
– Most instructions also overwrite CCRs, but LC3 gives no
instruction to save those aside, so program should not
expect old CCRs to be available
– ADD ..
– JSR .. (ADD's CCRs killed by JSR, so not seen by Brzp)
– Brzp .. won't work
• Load registers back from memory when the routine is done
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 12
Who saves aside program registers?

Caller of routine (caller-save) – knows what it needs later, but


may not know what will get altered
• Save needed registers destroyed by own instructions (e.g.,
JSR/JSRR/TRAP overwrite R7) or by called routine
• Restore after return

Called routine (callee-save) – knows what it alters, but may not


know what caller actually needs
• Start routine by saving registers to be overwritten
• Restore registers before return
• Much more common, but callee/caller save determined by
convention

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 13


Callee-save Terminal Routine

TERMINAL ST R0, SaveR0


ST R1, SaveR1
INPOLL LDI R0, KBSR
BRzp INPOLL
LDI R0, KBDR
OUTPOLL LDI R1, CRTSR
BRzp OUTPOLL
STI R0, CRTDR
LD R0, SaveR0
LD R1, SaveR1
RET
SaveR0 .FILL x0000
SaveR1 .FILL x0000

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 14


Input Arguments and Return Values

Some routines take input values from the main program


• Often termed arguments or parameters
Some routines give result values back to the main program
• Often termed return values

Example: mathematical routines


• MULTIPLY routine takes two inputs and produces a result
• SQRT routine takes an input and produces a result

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 15


Conventions for Arguments and Results

How to pass in arguments?


• How does callee know where each specific argument is?
How to retrieve return values?
• How does caller know where return value is?

Answer: use “software convention” e.g., argument 1 is in R0,


argument 2 is in R1 and return value is in R0
Conventions differ from system to system, but most pass inputs
and produce outputs in low-numbered registers
• If too many, use memory instead

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 16


Let’s make a routine!

String length
• Input: sequence of characters terminated by NUL (ASCII 0)
• Output: length of string
• We’ll use R0 as the base address of the input string and
also as the length field
Let’s write out the “algorithm/pseudocode/flowchart”for the
routine first

Read pages 219-229

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 17


String length routine pseudocode

Set length to 0
Is current string address pointing at NUL?
• If so, return length

Length = 0

H e l l o w o r l d !

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 18


String length routine pseudocode

Set length to 0
Is current string address pointing at NUL?
• If so, return length
• Else, increment length and string pointer; repeat

Length = 1

H e l l o w o r l d !

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 19


Converting pseudo-code to callee-save code
String address pointer passed into R0
Set length to 0 – put 0 in R1 (need to save R1 first!)
Is current string address pointing at NUL? – LDR to R2 sets
CCRs (but need to save R2 first!)
• If so, return length
– BRz, copy length into R0 (result), restore R1/R2, RET
• Else, increment length and string pointer; repeat
– Add #1 to length and input pointer, BRnzp

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 20


Code for STRLEN

STRLEN ST R1, SaveR1 ; save R1 (length)


ST R2, SaveR2 ; save R2 (test char)
AND R1, R1, #0 ; set length to 0
TEST LDR R2, R0, #0 ; check current char
BRz DONE ; found NUL
ADD R1, R1, #1 ; increment length
ADD R0, R0, #1 ; increment address
BRnzp TEST ; loop around
DONE AND R0, R1, R1 ; copy length into R0
LD R1, SaveR1 ; restore R1
LD R2, SaveR2 ; restore R2
RET ; return to main code
SaveR1 .FILL x0000
SaveR2 .FILL x0000
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 21
Invoking STRLEN

Remember that “.STRINGZ”directive creates a NUL-


terminated sequence of ASCII characters
.EXTERNAL STRLEN ; linker will resolve
LEA R0, Hello
LD R1, StrLen
JSRR R1, #0
ST R0, Length
HALT
Hello .STRINGZ “Hello world!”
StrLen .FILL STRLEN ; routine address
Length .FILL x0000

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 22


Different types of routines
Routines could be specific to a program or generic and
common across many programs
• e.g., interaction with I/O are common in many programs

Also some routines are special in that they access hardware


resources shared by many users
• e.g., many users use the same computer and put files on
the disk and we don't want one user to accidentally or
intentionally mess up another user's files
• So we need a controlled way to access shared resources
• Modern computers use the operating system (OS) (which
is a software program) to manage shared resources
• So user programs do not access the resources on their own
• Instead they call special routines which are in the OS
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 23
Different types of routines
Apart from sharing of resources, the special routines shield the
application programmer (who just wants to get data from I/O)
from the gory details of the I/O device
• So now the application programmer does not write the
routine but she simply calls the routine
Of course someone has to write the routines but that person is
the system programmer whose job is to know the gory details
of the I/O device
• System programmer does one thing and application
programmer does another thing
• Division of labor and specialization (common-sense ideas)

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 24


TRAP routines
The OS routines access the resource on behalf of the user
• There are sophisticated checks in the OS to prevent one
user from messing with another user's files
• The checks check whether the accessor has right level of
privilege (OS has higher privilege than user program)
The OS routines then return the data to the user program
• Just like a regular routine return's data to the caller

Called trap routines - we use TRAP instruction instead of a


JSR/JSRR, because the routines are not part of the user
program but are part of the OS
• Make a list of all the TRAP routines offered by the OS
• Put the routines' addresses in a table in memory
– lowest region of memory (near x0000) in LC-3)
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 25
TRAP routines
• The trap vector table location is fixed and known
• TRAP instruction specifies the trap number (trap vector)
– Different than JSR/JSRR which give offset or base reg
– table address + vector gives address of table entry that
holds TRAP routine address (think indirect mode)

See Fig 9.3

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 26


TRAP routines
• All other things for TRAP are same as JSR/JSRR
– Save incremented PC in R7
– Callee or caller save of registers
– TRAP routine returns via RET instruction
Chapter 5 eg had TRAP instructions for keyboard input (TRAP
x23), display output (TRAP x21)
A few slides ago, we saw a terminal polling routine which could
be thought of as the TRAP routine for keyboard input

© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 27


TRAP routine for Writing one character to
Display
.ORIG x0430 ; TRAP routine start address
ST R1, SaveR1 ; Save R1
; Write the character
TryWrite LDI R1, DSR ; Get status
Brzp TryWrite ; Test bit 15
STI R0, DDR ; Write it
; return
LD R1, SaveR1 ; Restore R1
RET
DSR . FILL xFE04
DDR .FILL xFE06
SaveR1 .BLKW 1
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 28
Different types of routines: Library
Like TRAP routine another example of generic common
routines is a library
• Library is a bunch of useful routines to be used by
application programmer
• Library is also programmed by systems programmer
• E.g.,math library where trigonometric functions are coded
so that application programmer can just use them without
having to learn gory details of trigonometric functions
Library is different from TRAP in that library does not deal with
I/O devices and is not in the OS
User programs call libraries via normal JSR/JSRR and RET
libraries are in separate files and linked in withuser program
• user code uses .EXTERNAL for library routines
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 29
Different types of routines
Textbook discusses TRAP first and then regular routines
I think the other way is easier

As an example we looked at fig 9.5 – a small TRAP routine


We won't go into OTHER details of TRAP routines
• Skip sections 9.1.5, 9.1.6 (except Fig 9.5)

Also look at regular routine examples (sections 9.2.4 and 9.2.5)


which are different from STRLEN routine shown in class

In 495k, we cover 9.1-9.2


Read pages 251-257
© 2009 Vijaykumar ECE495K Lecture Notes: Chapter 9 30

Potrebbero piacerti anche