Download Computer Organization & Programming: Subprograms - Structure, Writing, & Use - Prof. Rober and more Quizzes Computer Architecture and Organization in PDF only on Docsity!
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Lecture 5: Subprograms
Dragan Mirkovic
Department of Computer Science
University of Houston
Announcements
• Quiz #3 has been moved to 2/26/
– Control structures (Ch. 5 in text)
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Introduction
• Subprograms are one of the most important
structuring devices in your code
- Simple interface for reusing the same piece of code in different places and with different data.
- If well designed a subprogram should have a single purpose independent from the rest of the program.
- Well structured programs are easier to write, maintain and debug.
- We have used subprograms in the uitl library already.
• Interrupt services are special type of subprograms
with a specific interface
- Interrupt Service Routines (ISRs) in BIOS and DOS provide the lowest layer of software in MS-DOS operating system.
• In the assembly language, the basic structure of a
subprogram is very similar to the main program
structure
SubProg PROC … subprogram code … ret SubProg ENDP
• This subprogram is invoked by
Call SubProg
• The ret instruction returns the control to the
statement immediately following the the call
instruction
Subprogram structure
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Writing subprograms…
- For readability, data which is used only by the subprogram
should be physically associated with it. ... Main ENDP
.DATA ... SubProg data ... .CODE SubProg PROC ...
- In MASM (v. 6.0 and up) the labels are local to the PROC
unless ‘published’ with a PUBLIC statement.
- In some other assemblers labels are visible to all procedures in
a single source file (TASM)
Example
- Interrupts are procedures which, in general, change the state of
the registers
- A call to DOS INT 21h displays a string on the console and it
uses AX and DX registers
- One can save the register values by using stack .data message db ‘Hello!’, 13,10,’$’ .code push ax push dx mov ah, mov dx,offset message int 21h pop dx pop ax
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
The STACK
• The stack is a LIFO structure directly related to
subprograms
• When you allocate the stack, the stack pointer points
to its top (highest address)
• Two main operations
push mem/reg/const pop mem/reg
• The operand size must always be 16 or 32 bits
- push will store a word or a dword at ss:sp and decrease the value of the sp by 2 or 4.
- pop will copy a word or a dword from the memory position ss:sp and increase the value of sp by 2 or 4.
PUSH and POP Examples
• Only on 80286 and up can you push an immediate
value.
• Examples:
push ax ; save a 16-bit register push ecx ; save a 32-bit register push memval ; save a memory operand push 1000h ; save immediate value
• Examples:
pop ax ; restore a 16-bit register pop ecx ; restore a 32-bit register pop memval ; save a memory operand
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
The other stack related instructions
• The subroutine may change the state of the flags
register.
• The PUSHF instruction pushes the Flags register on
the stack.
• The POPF instruction is used to restore the flags
original state.
• Starting with the 80286 processor there are two more
stack related instructions
- PUSHA: pushes AX, CX, DX, BX, SP, BP, SI, DI.
- POPA: pops the same registers in reverse order.
- PUSHAD/POPAD: the same for EAX, EBX, …
Using STACK
• The main use of the
stack is to preserve a
set of registers which
define the state of the
processor
• Subprograms should
save and restore any
registers they use
• The standard way to do
that is to use the stack
• Subroutines should pop
all (and only) the items
they push on the stack
- Example SubProg PROC push ax push bx push cx ... pop cx pop bx pop ax ret SubProg ENDP
- Note: pop is in REVERSE order
- Example: push ax push bx pop ax pop bx
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
The stack, call and ret
• The stack is used to save the IP during the
execution of the subprogram.
• The effect of the call SubP instruction:
– Increase the value of the IP
– Push the IP register (saves the return address on
the stack).
– Jump to SubP
• The effect of the ret instruction is to pop the
IP from the stack.
– The next instruction executed is the one following
the call.
Nested subprograms
- A subprogram may itself call
other subroutines (including itself)
- The stack holds the list of
return addresses so the CPU can find its way back to the original call.
- The ret at the end of the
procedure pops the IP and execution resumes with the instruction following the call
.CODE
main PROC _Begin mov ax, A call subA _Exit 0 main ENDP subA PROC call subB; ret subA ENDP subB PROC call subC; ret subB ENDP subC PROC ret subC ENDP END main
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
• In the assembly language, the basic structure of a
subprogram is very similar to the main program
structure
SubProg PROC … subprogram code … ret SubProg ENDP
• This subprogram is invoked by
Call SubProg
• The ret instruction returns the control to the
statement immediately following the the call
instruction
Subprogram structure
Separately translated subprograms
- If a subprogram is of general use it may be advantageous to
save in a separate file.
- This requires a few extra statements.
- Example:
;;MAIN.ASM
INCLUDE PCMAC.INC
.MODEL SMALL
.STACK 100h .DATA ; data for the main prog .CODE EXTRN SubProg : NEAR main PROC _Begin call SubProg _Exit 0 main ENDP END main
;;SUB.ASM
INCLUDE PCMAC.INC; if necessary .MODEL SMALL . .STACK 100h .DATA ; data for SubProg .CODE PUBLIC SubProg SubProg PROC ; _Begin not needed ret SubProg ENDP END ; no label!!!
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Separately translated subprograms
• The two programs must be assembled separately
masm main; masm sub;
- If macros are used in SubProg, they need to be included in it.
- Normally, no stack is allocated in the sub. Any stack space specified is added to that in the main program
• Next, they need to be linked together
link main + sub,,,util.lib; (if needed)
• In some cases it is useful to store a collection of
object files in a library
lib command is used in MASM. (See text)
Symbol tables
• Assembler creates the symbol table
- Record programmer-defined symbols and information about them (location, storage, etc.)
- This information is used inside the translational unit and it is discarded at the end of the translation
- The EXTRN declaration causes the assembler to save each location where the external symbol is used so that the linker can later fill in the location
- .DATA used by the two programs must be separated into
- Data used by the main program only, which goes into MAIN.ASM
- Data used by the subprogram only, which goes into SUB.ASM
- Common data should use EXTRN/PUBLIC statemens
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Program libraries …
- Examples:
- create a new library NLIB.LIB containing files A.OBJ, B.OBJ, C.OBJ lib nlib +a +b +c
- Delete B.OBJ from NLIB.LIB lib nlib –b
- Replace A.OBJ and C.OBJ in NLIB.LIB with new versions lib nlib -+a -+c
-+ Replace file in library
- Delete file from the library
+ Add file to the library
Prefixes Actions
The Linking Process
• Assembler creates the symbol table
- Record programmer-defined symbols and information about them (location, storage, etc.)
- This information is used inside the translational unit and it is discarded at the end of the translation
- The PUBLIC and EXTRN declarations require special treatment
- Assembler saves two tables into the .OBJ file
- Table of EXTRN symbols with their reference points
- Table of PUBLIC symbols and its unique place of definition
• The linking process of attaching calls to their
destinations is called
resolving external references
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
The Linking Process…
• Example:
link P1+P2+…+Pn,,,L1+L2+…+Lm;
- Links P1+P2+…+Pn and uses L1+L2+…+Lm to resolve any further external references
- It performs the following steps:
- Read each Pi.OBJ and merge it into three objects:
- A file of object code OC. (If OC contained n bytes of code before, Pi’s code will start at offset n)
- A table of external references XSR. For each EXTRN symbol in Pi add its references to the end of XSR table and add n to each of the reference locations.
- Table of public symbol definitions PSD. For each PUBLIC symbol, check if it is already in PSD and generate an error message if necessary, otherwise add it to the end of PSD and add n to its location.
Resolving external references
2. For each symbol in XSR, look it up in PSD. If it
is there resolve all references for that symbol
and delete it from XSR.
3. If the XSR is now empty, all references are
resolved and we are done!
4. If not for each library Li.LIB, read through
each .OBJ file and see if it has any PUBLIC
symbols referenced in XSR. If so, read in that
.OBJ file as in step 1. Above.
5. If no new .OBJ files were added in 4., then
there are unresolved external references and
linking fails! Otherwise go back to step 2.
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
External symbols
- Object file contains three types of records describing the PUBLIC and EXTRN names: - EXTDEF External Names Definition Record - TYPDEF Type Definition Record - PUBDEF Public Names Definition Record
C:\Src\Asm>debug jtst.obj -d 0B1D:0100 80 0A 00 08 6A 74 73 74-2E 61 73 6D 3A 96 2B 00 ....jtst.asm:.+. ... 0B1D:0160 03 59 8C 09 00 06 50 55-54 44 45 43 00 A0 A0 15 .Y....PUTDEC.... 0B1D:0170 00 02 00 00 05 00 04 00-41 20 3D 20 24 42 20 3D ........A = $B =
- EXTDEF—External Names Definition Record Format
Object file records
• PUBDEF— Public Names Definition Record Format
- The PUBDEF record contains a list of public names.
- It makes items defined in this object module available to satisfy external references in other modules with which it is bound or linked.
- See OMF: Relocatable Object Module Format Specification for more details.
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Procedures and High-Level Languages
• Up to now we have used only very simple
procedures
– Little space required for parameters and local
variables
– No recursion and reentrancy
• Here we discuss most general requirements
for procedures and their use of the stack
• High-level languages require more
functionality from the stack
Procedures and the Stack
• Functionality required in high-level
languages:
– Passing of the parameters
- Registers may not be enough
– Storage for the local variables
– Each recursive call requires its own copy of the
local variable set.
– Many procedures need to be reentrant (usable by
several different programs simultaneously)
• Solution: the stack frame
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
The bp register
• Various parts of the stack frame can be
referenced relative to bp register
• The caller creates the 1 st^ part of the stack
frame by pushing the parameters and the
return address onto the stack.
• The called procedure creates the 2 nd^ part
– It saves and restores the bp register first
– Next, it saves its local parameters and registers
used inside the procedure
Setting up the stack
• Saving and resetting bp in the called procedure :
SubProg PROC push bp mov bp, sp ...
- bp contains the value of the stack pointer before the local variables and registers are pushed onto the stack.
• Allocate space for the local parameters:
... sub sp, n; Reserve n bytes of local ; storage push reg push reg ...
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Stack Structure in Sub
• The result is the following stack structure:
Saved registers
Local Variables
old bp
Return address
Parameters
n bytes
[bp + 6]: [bp + 4]: [bp + 2]:
bp
[bp - m]:
sp
- This is assuming the small model (call pushes only the IP )
- In medium and large models far call pushes CS and IP
Exit procedure
• Assuming that all temporary storage has been
released (popped of the stack) sp points to the last
saved register
...
pop reg
pop reg
mov sp, bp;
pop bp
• This code should always be used exactly in order to
avoid errors
- Some high-level languages need some additional structures