EEL 3801 Part IV The Assembler

advertisement
EEL 3801
Part IV
The Assembler
OFFSET Operator
• Returns address of variable used as operand.
• Actually, it represents the offset from the
beginning of the segment.
• The destination operand must be a 16-bit
register.
• When a register holds an address (offset)
rather than the value therein, it is a pointer
to that variable.
SEG Operator
• Returns the segment part of a label’s
address.
• The segment from which the offset is being
displaced.
• Not the offset.
Example
• Used when the base segment register must
be initialized to a new segment.
• Assume array is in a segment other than
what DS points to:
push
mov
mov
mov
.
pop
ds
;Save contents of DS
ax,seg array ; set DS to seg
ds,ax
; of array
bx,offset array ; set offset
; compute whatever
ds
; restore original DS
PTR Operator
• Specifies the size of an operand.
• Can override an operand’s default size
–
–
–
–
–
BYTE PTR (8-bit)
WORD PTR (16-bit)
DWORD PTR (32-bit)
QWORD PTR (64-bit)
TBYTE PTR (80-bit)
LABEL Directive
• Another way to override a variable’s default
type.
• Does not allocate more memory
• Just redefines an existing variable.
Transfer of Control Instructions
• CPU executes instructions sequentially, in
the exact order presented.
• Occasionally, we want a slightly different
order of execution,
– so must tell assembler so that it knows what is
the next instruction to load and decode prior to
execution.
Transfer of Control Instructions
(cont.)
• Two types:
– Conditional transfers
– Unconditional transfers.
Unconditional Transfers
• Program branches to new location all the
time, unconditionally.
• New value loaded into the instruction
pointer.
Conditional Transfer
• Branching only if certain conditions are
true.
• CPU interprets true/false condition based on
content of CX and Flags registers.
Some examples of these instructions:
JMP Instruction
• Tells CPU to begin execution at another
location.
• New location to be identified by label
• Label translated by assembler into new
address.
• If jump is to label in current segment, label’s offset
loaded into the IP register.
• If jump is to another segment, segment address
additionally loaded into CS.
JMP Instruction (cont.)
• Syntax is:
JMP SHORT/NEAR PTR/FAR PTR destination
where
SHORT => destination within –128 to 127 bytes
NEAR PTR => destination in same segment (default)
FAR PTR => destination in another segment
destination => a label or 32 bit segment-offset
address.
JMP Instruction (cont.)
• Can move to just about anywhere,
•
•
•
•
•
to same procedure,
to another procedure,
to another segment,
to RAM or ROM
or completely out of the current program.
• Loop based on JMP will never end.
LOOP Instruction
• Repeats a block of instructions a specific
number of times.
• Uses CX as a counter, and decrements it
every iteration.
• The number of cycles must be loaded into
CX prior to the loop taking place.
• Format is: LOOP destination
LOOP Instruction (cont.)
• Destination must be from –128 to 127 bytes
away from the current location.
• Destination typically behind loop
instruction.
• When it gets to LOOP, assembler
decrements CX by 1.
• If not zero, transfers control to destination.
Example #1
mov
next:
mov
mov
int
loop
cx,12*80
ah,2
;DOS function display char
dl,’A’
21h; Call DOS function on ah
next
Example #2
sum_an_array:
mov ax,0
mov di,offset intarray
mov cx,4
; array size
read_int:
add ax,[di]
; add integer to accum
add di,2
loop read_int
intarray dw 200h, 100h, 300h, 600h
Procedures
• It is generally infeasible to write a program
consisting of long sequence of instructions.
• True in assembly or high level language.
• More likely, blocks of code that do
something logically related
•
•
•
•
complex mathematical calculation,
setting up or retrieving a data structure,
printing something
reading a file.
Procedures (cont)
• By grouping and interacting these blocks of
code, a program is much easier to write,
debug and document.
• Moreover, the blocks of code may be
reusable either in the same program, or in
other programs.
• These blocks of code are called procedures,
or subroutines.
Procedures (cont)
• Procedures or subroutines represent a
branching of the program control when a
call to a subroutine is reached in the
program execution.
• The assembler must return to the instruction
after the one that called the subroutine.
Procedures (cont)
• The subroutine can be called through the
instruction CALL followed by the name of
the subroutine.
• CALL saves address of next instruction to
stack, depending on whether near or far call.
• This is different from the JMP instruction:
– JMP does not require a return to the original
address from where it was called.
Procedures (cont)
• Subroutines that return a value.
• They are typically equated to a label that
takes on the value resulting from the
function execution.
• Procedures do not normally return a value.
• PROC and ENDP are the bookends that
mark the beginning and end of a procedure.
Procedures (cont)
• These directives must include the name of
the procedure, which precedes the directive
itself.
• Procedures must include the RET (return)
instruction to pop old value of address from
run-time stack.
• Procedures must not be overlapped.
• See example on page 107 of text book.
Near and Far Procedures
• The difference between them is how the
assembler remembers where to return to.
Near Call
• Calling procedure and called procedure are
in the same segment of the program.
• Assembler generates machine code for a
near call.
• Before branching to the subroutine, the
CALL instruction preserves the current
content of the IP register (the instruction
pointer) onto the run-time stack.
Near Call (cont)
• Remember that the IP points to the next
instruction to be executed.
• It then copies the address of the first
instruction of the called subroutine into the
IP register.
• Thus, the CPU follows that sequence of
instructions that begins at that address.
Near Call (cont.)
• At the end of the subroutine, the RET
instruction pops the old value of IP into it
again from the stack.
• Execution then resumes right from where
the call was made.
Far Call
• This happens when the calling and called
subroutines are in different segments of the
code.
• The assembler then does a far call.
• Now the CALL instruction preserves both
the contents of the IP and the CS register
onto the stack.
• Uses a RETF instruction instead of RET.
Nested Calls
• Calls can be nested.
• The CALL instruction keeps the addresses
in the run-time stack on a last-in-first-out
basis, so there is no problem.
• The RET instructions are called in reverse
order.
• See example on page 113, Figure 5.7.
Interrupts
• There are reasons why the CPU needs to be
interrupted from its activity.
• Two types of interrupts:
– Hardware interrupts
– Software interrupts
Hardware Interrupts
• Are signals from other parts of the system
(hardware) that something needs immediate
attention from the CPU.
• These signals are generated by the Interrupt
Controller, a chip called the 8529IC.
• They allow for important events occurring
in the background to be “noticed” by the
CPU and immediately acted upon.
Hardware Interrupts - Examples
• Examples of hardware interrupts are inputs
that would be lost if not read and processed
quickly.
• Sources of such interrupts are:
– the keyboard,
– external memory (disk) drives,
– system clock.
The Interrupt Flag
• Occasionally, however, some processes are
highly time-sensitive and do not tolerate
any interrupts.
• The programmer can allow for this by
turning off the ability to interrupt the CPU.
• This is done through the interrupt flag, IF.
The Interrupt Flag (cont.)
• When the IF is set, interrupts are enabled
(normal value);
• When clear, interrupts are disabled.
• Instruction CLI clears the interrupt flag
(disallows interrupts),
• Instruction STI sets the interrupt flag (reenables interrupts).
Software Interrupts
• These are not really interrupts at all, but
rather calls to outside routines such as BIOS
or DOS functions.
• The INT instruction is used to introduce
such “interrupts”.
• format as follows:
INT number
The INT Instruction
• Calls a routine type identified by a number.
• The number can range between 0 and FFh.
• The routine type can be one of several, and
each type will have its own range of
possible functions.
• The number indicated as an operand of the
INT instruction then is referred to the
Interrupt Vector Table (IVT).
The INT Instruction (cont.)
• INT maps all the interrupt numbers to
operating system subroutine type.
• IVT located in the lowest 1KB of memory.
• Each entry is a 32-bit segment-offset
address, which points to the location where
the Operating System subroutine resides.
• Executes the routine at that address.
The INT Instruction (cont.)
• The various interrupt types are:
• INT 10h: Video services (cursor position, graphics,
scroll)
• INT 16h: Keyboard services (read keyboard and
check status)
• INT 17h: Printer services (initialize, print, return
printer status).
• INT 1Ah: Time of Day (no. of ticks since machine
turned on)
The INT Instruction (cont.)
• INT 1Ch: User Timer Interrupt (executed 18.2 times
per sec.)
• INT 21h: DOS services (DOS routines for file
handling, memory management, I/O, - DOS
function calls)
• The instruction IRET (interrupt return) tells
the processor to resume execution at the
next instruction of the calling program.
DOS Function Calls
• Called by INT 21h interrupt serv. routine
• Before the function type executes, it looks
in register AH to determine the function
number that identifies the subroutine itself
(NOT the type of interrupt).
• Listed in page 116, figure 5.9.
DOS Function Calls
• These numbers are the ones that must be
represented in register AH, as that is where
the processor looks to see what the function
number is.
• These are described in detail in the book.
• Please read over.
BIOS Level Video Control
Instructions
• The BIOS level video control instructions
are also listed in detail in the book.
• Please read them over.
Download