1 / 12

Chapter 6

EE314 Microprocessor Systems. Chapter 6. Objectives: Breaking down a large program into small tasks How to write a software driver Reading character strings from a keyboard Packing BCD digits into a byte Item search and lookup in a data table Comparison of data strings Sorting algorithms

roland
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EE314Microprocessor Systems Chapter 6 Objectives: Breaking down a large program into small tasks How to write a software driver Reading character strings from a keyboard Packing BCD digits into a byte Item search and lookup in a data table Comparison of data strings Sorting algorithms The use of condition flags to return results from a routine Binary and BCD math Writing a routine to perform a complex mathematical function Open- and closed-loops control systems (simplified theory) How to convert binary numbers to decimal numbers and reverse Insertion of an item into a linked list The operation of stacks and queues An Introduction to Programming the 80x86 Based on "An Introduction to the Intel Family of Microprocessors" by James L. Antonakos

  2. 6.2Tackling a Large Programming Assignment • Purpose (the task to do) • Restrictions (specific limitations: data size, parameters’ limits, excepted conditions, etc) • Input (data to be transferred form main program at routine call) • Output (data to be transferred to main program at return) • Needed resources (memory, stack, registers affected) The assignment in the form ofspecification Breaking down the program intomodules • each module is defined trough it’s own specification Structured programming • using a driver: supplies the subroutine with sample input, appeals the subroutine and verifies the routine’s output for correctness) Testingthe modules • describe the structure using the Pseudocode (a generic programming language: not compiled by a program, only used for describing the task in an intermediate way between human language and programming language) Creating thefinal module

  3. Pseudocode Standard Control Structures Example of translating in assembler language Name Structure Example if AL = 0 then NEXT CMP AL, 0 JZ NEXT RET NEXT: ... if-then if <condition> then <action> if AL  0 then NEXT else SUM CMP AL, 0 JNZ NEXT SUM: ... RET NEXT: ... if-then - else if <condition> then <action1> else <action2> initialize counter to 100 repeat GETDATA PROCDATA decrement counter until counter = 0 MOV BX,100 AGAIN: CALL GETDATA CALL PROCDATA DEC BX JNZ AGAIN repeat - until repeat <statements> until <condition> while char  ‘A’ do <statements> end-while WHILE: CMP AL, ‘A’ JZ NEXT ... JMP WHILE NEXT: ... while - do while <condition> do <statements> end-while

  4. Pseudocode Standard Control Structures Example of translating in assembler language Name Structure Example case selvalue of 0 : clear counter 1 : increment counter 2 : decrement counter CMP AL, 0 ;is it 0? JNZ C1 MOV BL, 0 ;clear counter JMP NEXT C1: CMP AL, 1 ;is it 1? JNZ C2 INC BL ;inc counter JMP NEXT C2: CMP AL, 2 ;is it 2? JNZ NEXT ;no matches DEC BL ;dec counter NEXT: ... case case <item> of <item1> : <statement 1> <item2> : <statement 1> ... <itemX> : <statement X> case selvalue of 1 : increment counter 2 : decrement counter otherwise : clear counter CMP AL, 1 ;is it 1? JNZ C2 INC BL ;inc counter JMP NEXT C2: CMP AL, 2 ;is it 2? JNZ OTHER ;no matches DEC BL ;dec counter JMP NEXT OTHER: MOV BL, 0 ;clear counter NEXT: ... case case <item> of <item1> : <statement 1> <item2> : <statement 1> ... <itemX> : <statement X> otherwise : <other st.>

  5. 6.3Writing a Software Driver MOV AL, X[3] ;load fourth test value CALL QUAD ;compute result CMP AX, Y[6] ;look for match JNZ BAD LEA DX, PASS JMP SEND BAD: LEA DX, FAIL SEND: MOV AH, 9 INT 21h .EXIT ;Program TESTQUAD.ASM: Software ; driver for QUAD procedure .MODEL SMALL .DATA X DB 0,1,10,100 Y DW 6, 9, 486, 49806 PASS DB ’Procedure passes.’, 0Dh, 0Ah, ’$’ FAIL DB ’Procedure fails.’, 0Dh, 0Ah, ’$’ .CODE .STARTUP MOV AL, X[0] ;load first test value CALL QUAD ;compute result CMP AX, Y[0] ;look for match JNZ BAD MOV AL, X[1] ;load second test value CALL QUAD ;compute result CMP AX, Y[2] ;look for match JNZ BAD MOV AL, X[2] ;load third test value CALL QUAD ;compute result CMP AX, Y[4] ;look for match JNZ BAD QUAD PROC NEAR MOV BL, AL ;save input value MUL BL ;compute X^2 MOV CL, 5 MUL CL ;compute 5X^2 XCHG DX,AX ;save result MOV AL, 2 MUL BL ;compute 2X SUB DX, AX ;compute 5X^2-2X XCHG DX, AX ;get result into AX ADD AX, 6 ;compute 5X^2-2X+6 RET QUAD ENDP END

  6. 6.6String Operations .CODE .STARTUP CALL COMREC .EXIT COMREC PROC FAR LEA BX,CMDS ;point to command table MOV DI,0 ;init index within JMPTB MOV CX,5;init loop counter for 5 cmds. NXTC: PUSH CX;save loop counter MOV CX,4;init loop cnt. for 4 chrs. in cmd. MOV SI,0 CHK: MOV AL,[BX+SI] ;get a table character CMP CMBUF[SI],AL;compare it with command JNZ NOM INC SI ;point to next character LOOP CHK;continue comparison POP CX ;match found, fix stack JMP JMPTB[DI];jump to command routine NOM: POP CX ;get loop counter back ADD BX,4 ;point to next command text ADD DI,2 ;and next routine address LOOP NXTC ;go check next command CMER: RET COMREC ENDP ;Program COMREC.ASM: ;A command recognizer. ;5 available commands, ;each 4 character wide, ;placed in .DATA, one after other, ;beginning at address CMDS. ;5 near routine addresses, ;one for each valid command, ;placed in .DATA, starting at ;address JMPTB. ;The program jumps to the routine ;who’s name was recognized. ;The actual command is 4 byte wide, ;stored in .DATA at address CMBUF. ;If no valid command is recognized, ;program jumps to label CMER. .MODEL SMALL .DATA CMDS DB ’EXAM’, ’DUMP’, ’RUN ’ DB ’STOP’, ’LOAD’ JMPTB DW DOEXAM, DODUMP DW DORUN, DOSTOP DW DOLOAD CMBUF DB 4 DUP (?)

  7. 6.6String Operations ;Program LASTNAME.ASM: Search database for ;last name of each person and displays it. ;Record: ends with ASCII character “0Dh”. ;Last Name begins after first “20h” in record ;ends before first ”,” in record. ;Database begins at adr. DBASE, ends at first “0” .MODEL SMALL .DATA DBASE DB 'Dave Guza, MPC, 2389, B26',0Dh DB 'James Antonakos, EET, 2356, B20',0Dh DB 'Mike Fisher, RWA, 2376, A19',0Dh DB 'Jennifer Indigo, CS/AT, 2676, A7',0Dh DB 'William Robinson, LIS, 2300, J2',0Dh DB 'Michele Tanner, ILY, 2143, B45',0Dh DB 0 ;end of database .CODE .STARTUP mov AX, seg DBASE mov DS,AX ;load address of data segment mov ES,AX ;set up extra segment lea DI,DBASE ;set up pointer to database NEXT: call LASTN ;display next person's last name cmp byte ptr [DI],0 ;end of DBASE reached? jnz NEXT ;repeat if no match .EXIT LASTN PROC NEAR mov AL,20H ;character to find mov CX,64 ;max length of record cld ;set auto increment repnz scasb ;skip to beg. of last name DSPN:mov DL,[DI] ;load string character cmp DL,',' ;past end of last name? jz FEND ;jump if so mov AH,2 ;displays character int 21H ;DOS call inc DI ;next character jmp DSPN ;and repeat FEND:mov DL,0DH ;output a CR mov AH,2 int 21H mov DL,0AH ;output a line feed mov AH,2 int 21H mov AL,0DH ;character to find mov CX,64 ;max length of record repnz scasb ;skip to end of record ret LASTNAME ENDP END

  8. 6.7Sorting a string ;Sorts “NV” bytes at adr.“VAL”, increasing order .MODEL SMALL .DATA VAL DB 7,10,6,3,9 NV DW 5 .CODE .STARTUP CALL SORT .EXIT SORT PROC FAR MOV DX,NV ;get number of data items DEC DX ;subtract one to start DOPASS: MOV CX,DX ;init loop counter MOV SI,0 ;init data pointer CHECK: MOV AL,VAL[SI];get first val CMP VAL[SI+1],AL;cmp with 2nd val JNC NOSWAP MOV BL,VAL[SI+1] ;swap elements MOV VAL[SI+1],AL MOV VAL[SI],BL NOSWAP: INC SI ;point to next val LOOPCHECK ;continue with pass DEC DX ;dec pass counter JNZDOPASS ;until finished RET SORT ENDP END first step 7, 10, 6, 3, 9 DX=4 CX=4 AL=7 cmp 10 no swap “10” CX=3 after 1st step 7, 10, 6, 3, 9 AL=10 cmp 6 swap 10,6 “10” CX=2 after 2nd step 7, 6, 10, 3, 9 AL=10 cmp 3 swap 10,3 “10” CX=1 after 3rd step 7, 6, 3, 10, 9 AL=10 cmp 9 swap 10,9 “10” CX=0 DX=3 after 4th step 7, 6, 3, 9 , 10 Val max. already al last position next loop goes only on first 4 values (3 steps)

  9. 6.11Data structures Linked lists Address of first byte in node 2: 2 bytes if near, 4 bytes if far. Content of a register Pointer to list A node, including data and pointer to next node First byte in node 2 Inserting an element on first position: Example: SI=1200 SI=3100 Inserting an element 1200 1200 7506 deleting an element from list 4502 4502 1400 1400 Adding an element 2044 5204 2044

  10. 6.11Data structures initial SP SP after PUSH SP after CALL 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 SP after POPs SP after add 2 BP points here 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 SP after PUSH SP before RET 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 00 00 00 46 21 14 Stacks Stack = LIFO (Last Input, First Output) list. A dangerous example ( not recommended): ... PUSH 12h ;prepare first operand. PUSH 34h ;prepare second operand CALL STAD ;add operands. 2114 POP AX ;peak lower word of result POP BX ;peak lower word of result … STAD PROC NEAR ;add the two words on stack ;before return address. ;Returns the double word ;result in the same place. MOV BP,SP ;save SP pointing RET address ADD SP,2 ;SP points second operand POP AX ;load second operand from stack POP BX ;load second operand from stack ADD AX,BX ;add operands in AX MOV BX,0 ;load 0 into BX ADC BX,BX ;add CF to BX NOM: PUSH BX ;save higher word of result PUSH AX ;save lower word of result MOV SP,BP ;restore SP for RET RET (an interrupt here destroys the stack)

  11. 6.11Data structures initial SP SP after PUSH SP after CALL 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 BP+4 BP+2 BP points here 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 SP before RET 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 00 00 00 46 21 14 Stacks A better way to do same job: ... PUSH 12h ;prepare first operand. PUSH 34h ;prepare second operand CALL STAD ;add operands. 2114 POP AX ;peak lower word of result POP BX ;peak lower word of result … STAD PROC NEAR ;add the two words on stack ;before return address. ;Returns the double word ;result in the same place. MOV BP,SP ;save SP pointing RET address ;ADD SP,2 instruction eliminated MOV AX,[BP+2];load second operand from stack MOV BX,[BP+4];load second operand from stack ADD AX,BX ;add operands in AX MOV BX,0 ;load 0 into BX ADC BX,BX ;add CF to BX NOM: MOV [BP+4],BX ;save higher word of result MOV [BP+2],AX ;save lower word of result ;MOV SP,BP instruction eliminated RET (SP not modified. INT uses stack below 0FFA)

  12. 6.11Data structures SI BP (reader) SP (writer) DI 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 Queues Queue = FIFO (First Input, First Output) list. SI content specify the begin address of queue (highest) DI content specify the end address of queue (lowest) SP pointer to write into queue (each PUSH automatically decrements SP) SI pointer to read from queue (BP has to be decrement explicitly) - difference between PUSH and MOV instructions should be observed) INQUEUE: CMP SP,DI ;need to wrap around? JNZ NOADJSP ;no MOV SP,SI ;yes, reload SP NOADJSP: PUSH AX ;write data into queue JMP NEXT OUTQUEUE: CMP BP,DI ;need to wrap around? JNZ NOADJBP ;no MOV BP,SI ;yes, reload BP NOADJBP: SUB BP,2 ;adjust read pointer MOV AX,[BP] ;read data of queue memory NEXT: ... The routine should verify other two conditions: - BP not pass SP (try to read unwritten data) - SP not pass BP (try to write over data not yet red) (an interrupt in this routine destroys the stack also)

More Related