Procedures - PowerPoint PPT Presentation

About This Presentation
Title:

Procedures

Description:

SAL of signed integer x n bit = SF, ZF, PF set accordingly. SAR Op,Length or SAL Op,length ... Internal Interrupt - Generated within Microprocessor itself. I/O request ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 42
Provided by: margaret94
Category:

less

Transcript and Presenter's Notes

Title: Procedures


1
Procedures
  • Programs so far have one main procedure and an
    external procedure
  • Main procedure is created and receives execution
    from the operating system. At the end of
    execution, control is returned to the operating
    system.
  • The external procedure is assembled separately
  • A subprocedures receives execution from another
    procedure. At end of execution the subprocedure
    returns control to the calling procedures.
  • Figure 5.1 p 222

2
Subprocedure Interface
  • Save state (data values, register values) of
    caller
  • Transfer control to subprocedure
  • Pass inputs to subprocedure
  • Return to calling procedure
  • Pass outputs to calling procedure
  • Restore state of calling procedure

3
State
  • All data (including registers) which reflects
    everything that had occurred to that point in
    execution
  • Save state - PUSH, PUSHF,PUSHA
  • Restore state - POP, POPF,POPA
  • Caller performed
  • Only need to save/restore needed registers
  • Save - just before call Restore - just after
    return
  • Subprocedure performed (EX_5_1 pp231-232)
  • Save all registers
  • Save - at beginning Restore - just before return
  • May be performed as part of the procedure
    definition (EX_5_3 p238)

4
Passing Parameters - Call by Value
  • Pass value but changes made to it in the
    subprocedure do not effect value of parameter in
    caller
  • Techniques (p227)
  • Caller places input values in registers
    subprocedure places Value of argument bound to
    parameter
  • output values in registers (EX EX_5_1, EX_5_2,
    PUTDEC, GETDEC)
  • Caller places values in stack subprocedure
    obtains from stack

5
Passing Parameters - Call by Location (Reference)
  • Address of argument bound to parameter
  • Changes made by called procedure do impact values
    of parameters in calling procedure
  • Techniques (p227)
  • Caller places address of arguments in registers
    subprocedure references arguments directly and
    changes if need be (EX PUTSTRNG, GETSTRNG)
  • Caller places addresses in stack subprocedure
    obtains from stack

6
Defining Procedure
  • NEAR - Subprocedure defined in the same segment
    as caller (EX_5_1)
  • ltnamegt PROC NEAR
  • . . .
  • ltnamegt ENDP
  • FAR - Subprocedure defined in different segment
    from caller (EX_5_2) NOTE Change for TASM from
    MASM!
  • PUBLIC - Makes subprocedure available to other
    modules
  • PUBLIC ltnamegt
  • ltnamegt PROC FAR
  • . . .
  • ltnamegt ENDP

7
Calling Procedure
  • CALL lttargetgt
  • Here target is the name of a defined subprocedure
  • Pushes the address of the next instruction (after
    the CALL) onto stack
  • Alter CSIP value (like is done with a jump) to
    reflect address of target
  • If procedure is defined external to this module
    then the procedure must be declared with the
    following prior to access
  • EXTRN ltnamegtFAR

8
Returning from Procedure
  • RET
  • Pops stack to (address to return to) CSIP pair

9
Using External Procdures (Summary)
  • Define procedure with FAR and PUBLIC
  • In procedure where module is called, indicate it
    as EXTRN
  • Assemble both modules separately
  • \tasm\bin\tasm /zi
  • LINK modules together. If the external reference
    can not be solved then an error occurs during the
    LINK
  • \tasm\bin\tlink /I /v module1 module2,,,io

10
Extra Credit (10 pts)
  • Due 3/23
  • Exercise stated on p 233

11
Macro
  • User defined operation code
  • Definition Identify the set of operations to be
    performed when this opcode is used
  • ltnamegt MACRO ltdummy argumentsgt
  • ltmacro bodygt
  • ENDM
  • Expansion When assembler finds a macro, replace
    it with actual operations
  • ltnamegt ltactual argumentsgt
  • Replace dummy arguments with actual arguments
  • EX_5_5

12
Bit Manipulation
  • Shift - Move bits to left (right) in register or
    memory
  • Logical Operations - Perform Boolean Operations
    on Bits
  • Why manipulate bits?

13
Shift
  • MSB - Most significant bit (7 or 15)
  • LSB - Least significant bit (0)
  • One Bit
  • Left i to i1 MSB to CF LSB depends
  • Right i to i-1 MSB depends LSB to CF
  • Multibit like multiple single bits
  • OF set (cleared) if MSB changed (unchanged)
  • May use CL to indicate number of bits to shift
  • EX_6_1

14
SAR/SAL - Arithmetic Shift
  • SAR - Sign bit copied into MSB (Fig 6.2)
  • SAR of signed integer x n bits
  • SAL - 0 into LSB (Fig 6.3)
  • SAL of signed integer x n bit
  • SF, ZF, PF set accordingly
  • SAR Op,Length or SAL Op,length
  • Op - 8/16 bit register or memory location
  • Length - 1 or CL (containing length to shift)

15
SHR/SHL - Logical Shift
  • SHR - MSB 0 (Fig 6.4)
  • SHR of unsigned integer x n bits
  • SHL - LSB 0
  • Same effect as SAL (Fig 6.3)
  • SHR of unsigned integer x n bits
  • SF, ZF, PF set accordingly
  • SHR Op,Length or SHL Op,length
  • Op - 8/16 bit register or memory location
  • Length - 1 or CL (containing length to shift)

16
ROR/ROL - Rotate
  • Operand viewed as circular array
  • ROR - LSB to MSB and CF (Fig 6.5)
  • ROL - MSB to LSB and CF (Fig 6.6)
  • ROR Op,Length or ROL Op,length
  • Op - 8/16 bit register or memory location
  • Length - 1 or CL (containing length to shift)

17
RCR/RCL - Rotate Through Carry
  • Operand viewed as circular array
  • CF viewed as part of operand (and circular array)
  • RCR - LSB to CF CF to MSB (Fig 6.7)
  • RCL - MSB to CF CF to LSB (Fig 6.8)
  • RCR Op,Length or RCL Op,length
  • Op - 8/16 bit register or memory location
  • Length - 1 or CL (containing length to shift)

18
Boolean Operations
19
Logical Operations
  • Two operands with output going into destination
  • 8 bit or 16 bit operands
  • AND ltdestgt,ltsourcegt
  • OR ltdestgt,ltsourcegt
  • XOR ltdestgt,ltsourcegt
  • OF, CF cleared SF, ZF, PF set accordingly
  • Allowable combinations of operands Fig 6.9
  • NOT ltdestgt
  • 8 bit or 16 bit operand
  • No flags changed

20
More Logical Operations
  • Bit test
  • Like AND but destination not changed
  • TEST ltdestgt,ltsourcegt
  • Allowable operands Fig 6.10
  • EX_6_2

21
Flag Operations
  • CMC - Complements CF bit
  • CLC/STC - Clears/Sets CF flag
  • CLD/STD - Sets/Sets DF flag
  • CLI/STI - Clears/Sets IF flag

22
Addressing Modes
  • Register - Ex MOV DX,AX
  • Immediate - Ex MOV DX,20
  • Direct - Ex MOV DX,TWENTY
  • TWENTY DW 20
  • Indirect - Value in register is address of memory
    location containing value to use.
  • Ex MOV DX,BP
  • SSBP memory location has value to use

23
Indirect Register Addressing
  • .. Indicates indirect use of register value
  • BX, DI, SI registers used as offset to DS
    register for segment
  • BP associated with SS register
  • Override default segment with prefix
  • ESBX
  • SSBX

24
Memory As An Array
  • Ex 1
  • TABLE DW 10 DUP(?) LEA
    DI,TABLE MOV CX,10LOOPP
    DI,5 ADD DI,2
    LOOP LOOPP
  • Ex 2
  • TABLE DW 10 DUP(?) MOV
    DI,0 MOV CX,10LOOPP
    TABLEDI,5 ADD DI,2
    LOOP LOOPP

25
String Operations
  • String - Array of characters (byte or word)
  • DSSI - Source array
  • ESDI - Destination array
  • DF indicates if SI and DI are to be incremented
    (0) or decremented (1) after operation
  • Byte operations increment/decrement by 1 Word
    by 2.
  • CLD/STD - Clear/Set DF flag
  • MOVSB/MOVSW - Move byte/word at DSSI location to
    ESDI location

26
Additional String Operations
  • LODSB/LODSW - Load AL/AX with byte/word at DSSI
    location
  • STOSB/STOSW - Store byte/word from AL/AX to ESDI
    location
  • CMPSB/CMPSW - Compare the two bytes/words
  • Flags are set based on source (DSSI) minus
    destination (ESDI)
  • SCASB/SCASW - Compare byte/word in AL/AX to that
    at location ESDI

27
Repetitive String Operations
  • Repeat string operation using CX to indicate how
    many times
  • Fig 8.9 p386 Fig 8.10 p 388 Fig 8.11 p 390
  • REP xx - where xx is a string operation
  • REPE/REPZ xx - where xx is compare or scan
    repeat xx until CX 0 or ZF0 where ZF set by
    comparison.
  • REPNE/REPNZ xx - where xx is compare or scan
    repeat xx until CX 0 or ZF 1 by comparison

28
EXAMPLE
  • TABLE DW 10 DUP(?)
  • LEA DI,TABLE
  • CLD MOV AX,0
    MOV CX,10
  • LOOPP STOSB
  • LOOP LOOPP
  • TABLE DW 10 DUP(?)
  • LEA DI,TABLE
  • CLD MOV AX,0
    MOV CX,10
  • REP STOSW

29
Comparing Strings
  • Compare character by character
  • Ex
  • STRNG1 DB 5 DUP(?)
  • STRNG2 DB 5 DUP(?) MOV
    CX,5 LEA SI,STRNG1
    STRNG1 in DS LEA
    DI,STRNG2 STRNG2 in ES
    REPE CMPSB JE EQL
    JB NEQLEQL
    ... What to do if
    two are equalNEQ ...
    What to do if two are not equal

30
Interrupt
  • Break in normal Fetch-Decode-Execute Cycle (p4)
  • Request for special service of Operating Systems
  • Facilitates multiple programs executing at the
    same time
  • Allows overlap of I/O processing and program
    execution
  • Interrupts are checked for at the end of each
    instruction execution
  • When an interrupt is detected a special interrupt
    handler routine is initiated
  • IRET - return to procedure which was interrupted
  • How have multiple sets of registers????
  • p430

31
Interrupt Classes
  • External Interrupt - Caused by device external to
    Microprocessor
  • I/O Completion
  • Keyboard
  • Timer
  • Internal Interrupt - Generated within
    Microprocessor itself
  • I/O request
  • End of program execution
  • Divide by 0
  • INT xx - Causes internal interrupt of type xx
  • Subfunction in AH

32
More on Interrupts
  • Interrupt Type (0-255)
  • Uniquely identifies interrupt handler
  • First 1024 bytes in memory consist of interrupt
    vectors
  • Addresses of the interrupt handler routines
  • Maskable - Interrupt can be ignored
  • Nonmaskable - Interrupt can not be ignored

33
Some Predefined interrupts
  • 0 - Divide Overflow
  • 1 - Single Step
  • Used by debugger to trace program execution
  • Causes interrupt after each instruction is
    executed if TF set
  • 2 - Parity Error (Nonmaskable)
  • 3 - Breakpoint
  • 4 - Overflow
  • Causes interrupt if OF set
  • 8 - Timer
  • 9 - Keyboard

34
May Override Predefined Interrupt Service Handler
  • Type 0 Ex_9_1a
  • Type 1 Ex_9_2

35
BIOS Interrupt
  • Basic Input/Output system
  • Simple I/O procedures in ROM (Read Only Memory)
  • Invoked by INT instruction
  • Table 9.2 p447
  • Video I/O - INT 10H
  • Table 9.3 p448
  • Ex_9_3
  • Keyboard I/O - INT 16H
  • Table 9.3 p448
  • Ex_9_4

36
Disk File Usage
  • Create (new file) - INT 21H (3CH or 5BH)
  • Open (existing or overwrite) - INT 21H (3DH)
  • Read - INT21H (3FH)
  • Write - INT21H (40H)
  • Close - INT 21H (3EH)

37
Disk File Create INT 21H
  • Structure - Fig 9.1 p 470
  • Subfunctions
  • 3CH - if file exists, allows overwrite
  • 5BH - if file exists, open create fails
  • Input
  • DSDX has disk drive, filename,file extension,
    null
  • CX indicates type (all 0s for normal file) - p470
  • Output
  • CF 0 if successful, 1 otherwise
  • AX handle to be used for later access or error
    - p471
  • Example p 471

38
Disk File Open INT 21H
  • Allow access to existing file
  • Subfunction 3DH
  • Input
  • DSDX has name
  • AL has access code - p472
  • Output
  • CF 0 if successful, 1 otherwise
  • AX Handle or error - p 472

39
Disk File Close INT 21H
  • Terminate access to file
  • Subfunction 3EH
  • Input file handle in BX
  • Output in CF 0 if successful, 1 otherwise
  • Error in AX 6 if invalid handle
  • Ex p 473

40
Disk File Read INT 21H
  • Read a byte at a time
  • Subfunction 3FH
  • Input
  • BX - file handle
  • CX - number of bytes to read
  • DSDX - address of where to read bytes into
  • Output
  • CF - 0 if successful, 1 otherwise
  • AX - number of bytes actually read or error code
    - p 474
  • Ex p 474

41
Disk File Write INT 21H
  • Write a byte at a time
  • Subfunction 40H
  • Input
  • BX - file handle
  • CX - number of bytes to write
  • DSDX - address of where to write bytes from
  • Output
  • CF - 0 if successful, 1 otherwise
  • AX - number of bytes actually written or error
    code - p 475
  • Ex p 475
Write a Comment
User Comments (0)
About PowerShow.com