EEL 3801 - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

EEL 3801

Description:

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. – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 43
Provided by: AvelinoJ6
Learn more at: http://www.cs.ucf.edu
Category:
Tags: eel

less

Transcript and Presenter's Notes

Title: EEL 3801


1
EEL 3801
  • Part IV
  • The Assembler

2
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.

3
SEG Operator
  • Returns the segment part of a labels address.
  • The segment from which the offset is being
    displaced.
  • Not the offset.

4
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 ds Save contents of DS mov ax,seg array
    set DS to seg mov ds,ax of array mov
    bx,offset array set offset
  • . compute whatever
  • pop ds restore original DS

5
PTR Operator
  • Specifies the size of an operand.
  • Can override an operands default size
  • BYTE PTR (8-bit)
  • WORD PTR (16-bit)
  • DWORD PTR (32-bit)
  • QWORD PTR (64-bit)
  • TBYTE PTR (80-bit)

6
LABEL Directive
  • Another way to override a variables default
    type.
  • Does not allocate more memory
  • Just redefines an existing variable.

7
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.

8
Transfer of Control Instructions (cont.)
  • Two types
  • Conditional transfers
  • Unconditional transfers.

9
Unconditional Transfers
  • Program branches to new location all the time,
    unconditionally.
  • New value loaded into the instruction pointer.

10
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

11
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, labels
    offset loaded into the IP register.
  • If jump is to another segment, segment address
    additionally loaded into CS.

12
JMP Instruction (cont.)
  • Syntax is
  • JMP SHORT/NEAR PTR/FAR PTR destination
  • where
  • SHORT gt destination within 128 to 127 bytes
  • NEAR PTR gt destination in same segment
    (default)
  • FAR PTR gt destination in another segment
  • destination gt a label or 32 bit segment-offset
    address.

13
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.

14
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

15
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.

16
Example 1
  • mov cx,1280
  • next
  • mov ah,2 DOS function display char
  • mov dl,A
  • int 21h Call DOS function on ah
  • loop next

17
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

18
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.

19
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.

20
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.

21
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.

22
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.

23
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.

24
Near and Far Procedures
  • The difference between them is how the assembler
    remembers where to return to.

25
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.

26
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.

27
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.

28
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.

29
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.

30
Interrupts
  • There are reasons why the CPU needs to be
    interrupted from its activity.
  • Two types of interrupts
  • Hardware interrupts
  • Software interrupts

31
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.

32
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.

33
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.

34
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
    (re-enables interrupts).

35
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

36
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).

37
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.

38
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)

39
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.

40
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.

41
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.

42
BIOS Level Video Control Instructions
  • The BIOS level video control instructions are
    also listed in detail in the book.
  • Please read them over.
Write a Comment
User Comments (0)
About PowerShow.com