eel 3801 part iv the assembler. offset operator returns address of variable used as operand....

42
EEL 3801 Part IV The Assembler

Upload: clifford-richards

Post on 17-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

SEG Operator Returns the segment part of a label’s address. The segment from which the offset is being displaced. Not the offset.

TRANSCRIPT

Page 1: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

EEL 3801

Part IV The Assembler

Page 2: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 3: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

SEG Operator

• Returns the segment part of a label’s address.

• The segment from which the offset is being displaced.

• Not the offset.

Page 4: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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 DSmov ax,seg array ; set DS to segmov ds,ax ; of arraymov bx,offset array ; set offset. ; compute whateverpop ds ; restore original DS

Page 5: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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)

Page 6: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

LABEL Directive

• Another way to override a variable’s default type.

• Does not allocate more memory• Just redefines an existing variable.

Page 7: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 8: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

Transfer of Control Instructions (cont.)

• Two types:– Conditional transfers – Unconditional transfers.

Page 9: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

Unconditional Transfers

• Program branches to new location all the time, unconditionally.

• New value loaded into the instruction pointer.

Page 10: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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:

Page 11: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 12: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

JMP Instruction (cont.)

• Syntax is:

JMP SHORT/NEAR PTR/FAR PTR destination

where SHORT => destination within –128 to 127 bytesNEAR PTR => destination in same segment (default)FAR PTR => destination in another segmentdestination => a label or 32 bit segment-offset

address.

Page 13: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 14: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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

Page 15: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 16: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

Example #1

mov cx,12*80next:mov ah,2 ;DOS function display charmov dl,’A’int 21h ; Call DOS function on ahloop next

Page 17: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

Example #2

sum_an_array:mov ax,0mov di,offset intarraymov cx,4 ; array sizeread_int:add ax,[di] ; add integer to accumadd di,2loop read_intintarray dw 200h, 100h, 300h, 600h

Page 18: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 19: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 20: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 21: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 22: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 23: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 24: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

Near and Far Procedures

• The difference between them is how the assembler remembers where to return to.

Page 25: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 26: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 27: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 28: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 29: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 30: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

Interrupts

• There are reasons why the CPU needs to be interrupted from its activity.

• Two types of interrupts:– Hardware interrupts– Software interrupts

Page 31: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 32: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

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

Page 34: 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 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).

Page 35: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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

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

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

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

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

Page 40: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 41: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

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.

Page 42: EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of

BIOS Level Video Control Instructions

• The BIOS level video control instructions are also listed in detail in the book.

• Please read them over.