Presentation is loading. Please wait.

Presentation is loading. Please wait.

BITS Pilani Pilani Campus Pawan Sharma Lecture 29-32 09/12/14-03-2012 EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing.

Similar presentations


Presentation on theme: "BITS Pilani Pilani Campus Pawan Sharma Lecture 29-32 09/12/14-03-2012 EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing."— Presentation transcript:

1 BITS Pilani Pilani Campus Pawan Sharma Lecture 29-32 09/12/14-03-2012 EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing

2 BITS Pilani, Pilani Campus  Program Models  LOOP instructions  Signed multiplication and division  In/OUT instr  PUSH/POP  CALL instruction Last Lecture

3 BITS Pilani, Pilani Campus  Instruction Timing and Delay Loops  Procedures  Passing parameters to procedures Today’s Lecture

4 BITS Pilani, Pilani Campus Instruction Timing and Delay Loops

5 BITS Pilani, Pilani Campus Subroutine commonly used is delay subroutine Software delays using LOOP Delay required = 1ms =1000µs 8086 running on 5MHz clock- Each cycle is 0.2µs  Total of 5000 clock cycles required=C T Introduce the instructions: MOV CX,N 4 Cycles X1: NOP 3 Cycles NOP 3 Cycles LOOP X1 17 or 5 Cycles

6 BITS Pilani, Pilani Campus Overhead = C O = 4 Number of cycles/loop = C L = 3+3+17 = 23 Number of times the loop runs = N C T = C 0 + N*C L – 12 5000 = 4 + N*23 –12  N = 218

7 BITS Pilani, Pilani Campus Write an ALP that takes 100 data samples from input port, at intervals of 1ms and masks the upper four bits of each sample and puts each masked sample in successive memory locations in ARRAY –using subroutines

8 BITS Pilani, Pilani Campus In Assembly language programming, Procedures (subroutines) begin with PROC directive and ends with ENDP directive PROC directive is followed by the type of procedure: NEAR or FAR CALL instruction links to the procedure RET instruction returns from the procedure

9 BITS Pilani, Pilani Campus CALL SUMS _____ SUMS PROC NEAR ADD AX, BX ADD AX, CX ADD AX, DX RET SUMS ENDP

10 BITS Pilani, Pilani Campus.Model Tiny.data array db100 dup(0) stack dw100 dup(?) ; get aside 100 words for stack.code.startup leadi, array movcx,100 x1:inal, 05 h callmask call delay loopx1.exit

11 BITS Pilani, Pilani Campus call – 19 ret – 16 Overhead = CO = 4 +19+16 No. of cycles/loop CL = 3+3+17 = 23 No. of times the loop runs = N CT = C0 + N*CL – 12 5000 = 39 + N*23 –12  N = 217 Maskprocnear andal,0f h stosb ret Maskendp Delayprocnear movcx,217 x2: nop nop loop x2 ret Delayendp end

12 BITS Pilani, Pilani Campus Passing Parameters to Procedures  Parameters are data values or addresses passed back and forth between the mainline program and the procedure  Four ways to pass parameters  In Registers  In Dedicated memory locations accessed by name  With pointers passed in registers  With stack

13 BITS Pilani, Pilani Campus CALL SUMS _____ SUMS PROC NEAR PUSHBX PUSHCX PUSHDX ADD BX, AX ADD CX, BX ADDDX,CX ADD AX, DX POPDX POPCX POPBX RET SUMS ENDP Save the contents of the registers on the stack which are used by procedure and at the end, retrieve the saved values. -- good practice to push the flags and any registers used in a procedure, directly in the procedure

14 BITS Pilani, Pilani Campus ;Initialize segment registers DATASEGMENT BCD_INPUTDB 16h BIN_VALUEDB ? DATA ENDS STACK_SEG SEGMENT DW 100 DUP(0) STACK_SEG ENDS CODE SEGMENT START: MOV AX,DATA MOV DS,AX MOV AX,STACK_SEG MOV SS,AX MOV AL,BCD_INPUT CALL BCD_BIN MOV BIN_VALUE, AL BCD to Binary conversion (using parameter (data) in register) The BCD number is copied from memory to the AL and then passed to the procedure in the AL register. Conversion example: Packed BCD – 0001 0110 Decimal - 16 Bin/hex -10 h (01 * 0a h ) + 6

15 BITS Pilani, Pilani Campus EX: BCD to Binary Conversion BCD_BIN PROC NEAR PUSHF PUSH BX PUSH CX MOV BL, AL AND BL, 0F H AND AL, F0 H MOV CL, 04 ROR AL, CL MOV BH, 0A H MUL BH ADD AL, BL POP CX POP BX POPF RET BCD_BIN ENDP Parameter stored in register Not pushing the AX register on stack as we use it to pass a value to the procedure and expect the procedure to pass a different value back to the program Disadvantage: no of registers limit the no of parameters that can be passed to procedures.

16 BITS Pilani, Pilani Campus In the preceding example, why didn’t we simply access the BCD_INPUT and BIN_VALUE by name from the procedure? Passing parameters stored in general memory ;same data structure and initialization as in the previous example CALL BCD_BIN BCD_BIN PROC NEAR PUSHF PUSH AX PUSH BX PUSH CX MOV AL, BCD_INPUT MOV BL, AL AND BL, 0F H AND AL, 0F0 H MOV CL, 04 ROR AL, CL MOV BH, 0A H MUL BH ADD AL, BL MOVBIN_VALUE, AL POP CX POP BX POPAX POPF RET BCD_BIN ENDP

17 BITS Pilani, Pilani Campus  Can be done but with severe limitation!!  Procedure will always look for the named memory location BCD_INPUT to get its data and will always put the result in BIN_VALUE.  Or, we cant use this procedure to convert the BCD no stored somewhere else in memory.

18 BITS Pilani, Pilani Campus Passing parameters using pointers EX: BCD to Binary Conversion BCD_BIN PROC NEAR PUSHF PUSH AX PUSH BX PUSH CX MOV AL, [SI] MOV BL, AL AND BL, 0F H AND AL, 0F0 H MOV CL, 04 ROR AL, CL MOV BH, 0A H MUL BH ADD AL, BL MOV[DI], AL POP CX POP BX POPAX POPF RET BCD_BIN ENDP More versatile than using named memory location as the procedure pointers can point to data anywhere in memory. Set up SI and DI acting as pointers. MOV SI, OFFSET BCD_INPUT MOV DI, OFFSET BIN_VALUE CALL BCD_BIN

19 BITS Pilani, Pilani Campus  Push parameters on stack in mainline program before calling the procedure  Instructions in procedure read these parameters from stack as needed.  Parameters passed back from procedure are written on stack and read by the main-line program. Passing parameters using stack

20 BITS Pilani, Pilani Campus.model tiny.data bcd_input db 16h bin_value db (0).code.startup MOV AL,BCD_INPUT PUSH AX CALL BCD_BIN POP AX MOV BIN_VALUE, AL ;EX: BCD to Binary Conversion BCD_BIN PROC NEAR PUSHF PUSH AX PUSH BX PUSH CX PUSH BP MOV BP, SP MOV AX, [BP + 12] MOV BL, AL AND BL, 0F H AND AL, 0F0 H MOV CL, 04 ROR AL, CL MOV BH, 0A H MUL BH ADD AL, BL MOV[BP + 12], AX POP BP POP CX POP BX POPAX POPF RET BCD_BIN ENDP.exit end

21 BITS Pilani, Pilani Campus MOV AL, BCDINPUT CALL BCD_BIN Using reg BCD_BIN PROC NEAR MOV AL, BCDINPUT Using Mem MOV SI, OFFSET BCDINPUT CALL BCD_BIN BCD_BIN PROC NEAR MOV AL, [SI] Using Pointers

22 BITS Pilani, Pilani Campus Advantage: reduces the code length for repetitive usage of a group of instructions – Machine code generated only once Disadvantage: need stack and overhead time – NOT SUITABLE if repetitive instructions are short Solution: use MACROS Advantages & Disadvantages

23 BITS Pilani, Pilani Campus  A macro is a group of instructions that perform a task, just as procedure performs one task.  The difference is that the procedure is accessed via a CALL instruction, whereas a macro and all the instructions defined in the macro are inserted in the program at the point of usage.  Name of macro and any parameters associated with it are typed, and the assembler then inserts them into the program.  Macro is faster than procedure because there is no CALL and RET.  MACRO and ENDM are the keywords MACROS

24 BITS Pilani, Pilani Campus MOVE MACRO A, B PUSH AX MOV AX, B MOV A, AX POP AX ENDM MOVE VAR1, VAR2 ; use the MOVE macro PUSH AX MOV AX, VAR2 MOV VAR1, AX POP AX MOVE VAR3, VAR4 PUSH AX MOV AX, VAR4 MOV VAR3, AX POP AX

25 BITS Pilani, Pilani Campus Important to note that machine code is generated for every MACRO call Coding is easier but code length increases MACRO sequences must always be defined before they are used in the program, so they appear at the top of the segment.

26 BITS Pilani, Pilani Campus Ex: Block Move MOVE_ASCII MACRO NUMBER, SOURCE, DESTINATION MOV CX, NUMBER LEA SI, SOURCE LEA DI, DESTINATION CLD REP MOVSB ENDM

27 BITS Pilani, Pilani Campus MOVE_ASCII 003DH, BLOCK_START, BLOCK_DEST Results in: MOV CX, 003DH LEA SI, BLOCK_START LEA DI, BLOCK_DEST CLD REP MOVSB

28 BITS Pilani, Pilani Campus SAHF Store AH to Flag Register (Lower) LAHF Load Flag into AH Register (Lower) CMC Complement Carry Flag STI CLI

29 BITS Pilani, Pilani Campus LES REGISTER, MEMORY ADDRESS OF FIRST WORD  Put new values into the specified register and ES register from four successive memory locations.  LES BX, [7890H]  Loads BX with a word taken from DS at 7890H and 7891H  Loads ES register with a word taken from DS at 7892H and 7893H  LES DI, [BX]  Used to point DI and ES at start of string before a string inst is executed.

30 BITS Pilani, Pilani Campus LDS REGISTER, MEMORY ADDRESS OF FIRST WORD Same as LES but uses DS register in place of ES register LDS BX, [4325H] Loads BX with a word taken from DS at 4325H and 4326H Loads DS register with a word taken from DS at 4327H and 4328H

31 BITS Pilani, Pilani Campus XCHG Destination, Source  Exchange the contents of a reg with another reg  Exchange the contents of a reg with Memory location  Source & destination both must be of type word/bytes  Cannot directly exchange the contents of two memory locations.  Segment registers can not be used. XCHG AX, DX XCHG BL, CH XCHG AL, PRICES [BX]

32 BITS Pilani, Pilani Campus HLT  Stops execution of s/w  Stop fetching and executing instructions. Processor can come out of halt by  Interrupt  Reset  DMA request

33 BITS Pilani, Pilani Campus Interrupt Program Execution– INT type The term type refers to a number between 0 and 255 (00H to FFH), which identifies the interrupt. INT 21H - DOS INTERRUPT INT 10H – BIOS INTERRUPT INT nn instruction

34 BITS Pilani, Pilani Campus

35


Download ppt "BITS Pilani Pilani Campus Pawan Sharma Lecture 29-32 09/12/14-03-2012 EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing."

Similar presentations


Ads by Google