1 / 17

An Introduction to Programming the 80x86

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

mya
Télécharger la présentation

An Introduction to Programming the 80x86

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

  2. Tackling 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. Writing 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. String 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. String 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. Sorting 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. Data 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. Data 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. Data 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. Data 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)

  13. Microprocessor Systems Objectives: The use of DOS and BIOS function call How to read the PC’s keyboard How to send text to the video display The operation of the PC’s speaker How to control the printer The structure of the command segment prefix How to use DOS’s command-line interface Programming with DOS and BIOS Function Calls Based on "An Introduction to the Intel Family of Microprocessors

  14. Introduction Applications (.com or .exe files) DOS BIOS Your .exe file (under debugging) Hardware system BIOS functions • INT10 Video services • INT13 Disk Services • INT16 Keyboard functions • INT17 Parallel printer functions BIOS functions (Basic Input/ Output System) DOS functions Reserved INTs • keyboard • display • printer • disk • date/time • memory management • program control PWB.EXE DOS functions (Disk Operating System) • INT21

  15. Using the Keyboard DOS INT 21, function 01H:Wait for Keyboard Input Specification: waits for the user to press a key on the keyboard and returns the ASCII code. Input: AH = 01 (function code) Output: AL = ASCII code of the pressed key. The character is echoed to the video display. Constrain: doesn’t return the control to the main program until a key is pressed. If the key correspond to an extended ASCII code, AL returns 00. The next INT 21, function 01 returns in AL the extended ASCII code. DOS INT 21, function 08H: Console Input without Echo Specification: similar to function 01 but no echo on video display. BIOS INT 16, function 00H: Read keyboard Input Specification: similar to INT21function 01 butif the pressed key correspond to an extended ASCII code, AL returns 00 and AH returns the extended ASCII code. No echo to display. BIOS INT 16, function 01H: Read keyboard status Specification: doesn’t wait. If the keyboard buffer is empty, ZF is set to 1. If not, returns the first ASCII code from buffer in the same way like function 00, and clear ZF. BIOS INT 16, function 02H:Return Shift Flag Status Specification: waits for the user to press a key on the keyboard and returns the ASCII code. Input: AH = 02 (function code) Output: AL = Status of the special function keys: B7=Insert, B6=Caps Lock, B5=Num Lock, B4=Scroll Lock (active bit=1 => function active) B3=Alt, B2=Ctrl, B1=Left Shift, B0=Right Shift (active bit=1 => button pressed )

  16. Controlling the Video Display DOS INT 21, function 02H:Display Output Specification: writes a single character to the display screen, at the current cursor position. Input: AH = 02 (function code), DL = ASCII character to be sent to display. Control characters perform their specific action (0DH = Carriage Return, 0AH = Line Feed, 08H = Backspace, etc.). DOS INT 21, function 09H: Display String Specification: Send to display a string in the current data segment. The string ends with ’$’ character (not displayed). Input: AH = 09 (function code), DX = The offset of the first character in the string. BIOS INT 10, function 00H: Set Video Mode Specification: set video mode of the display (ex: mode 1 = 25 linesX40 characters, mode 3 = 25 linesX80 characters). Input: AH = 00 (function code), AL = The desired video mode . BIOS INT 10, function 0FH: Read Current Video Mode Specification: returns video mode of the display. Input: AH = 0F (function code) Output: AL = The current video mode. BIOS INT 10, function 02H:Set Cursor Position Specification: moves the cursor to specified position (in text mode). Input: AH = 02 (function code), DH = the row (0-24), DL column (0-79), BH = page (0) BIOS INT 10, function 03H:Read the Current Cursor Position Input: AH = 02 (function code), BH = page (0) Output: DH = the row (0-24), DL column (0-79)

  17. Controlling the Video Display BIOS INT 10, function 0AH: Write Character to Screen Specification: write multiple times a character to screen at current cursor position. Input: AH = 0A (function code), AL = ASCII code, BH = page number, CX = repeat value. BIOS INT 10, function 09H: Write Character/Attribute to Screen Specification: write multiple times a character to screen at current cursor position. Specify the video attribute of the character: B7 = blink, (B6 = red, B5 = green, B4 = blue)=background, B3 = intensity, (B2 = red, B1 = green, B0 = blue)=foreground Input: AH = 09 (function code), AL = ASCII code, BH = page number, BL = character’s attribute, CX = repeat value. BIOS INT 10, function 08H:ReadCharacter/Attribute from Screen Input: AH = 08 (function code), BH = display page (0) Output: AL = The Character code at the current cursor position, AH = the attribute byte. BIOS INT 10, function 06H:Scroll Current Page Up Input: AH = 06 (function code) AL = Number of rows to scroll up (0 for entire region) BH = attribute for scrolled region CH = Row number at top of region CL = Column number at left of the region DH = Row number at bottom of region DL = Column number at right of the region Examples: in the textbook!

More Related