Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Programming the 80x86 Objectives: Breaking down a large program into small tasks How to write a software driver Reading character strings.

Similar presentations


Presentation on theme: "An Introduction to Programming the 80x86 Objectives: Breaking down a large program into small tasks How to write a software driver Reading character strings."— Presentation transcript:

1 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 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 Based on "An Introduction to the Intel Family of Microprocessors

2 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) Tackling a Large Programming Assignment The assignment in the form of specification Breaking down the program into modules each module is defined trough it’s own specification Testing the modules using a driver: supplies the subroutine with sample input, appeals the subroutine and verifies the routine’s output for correctness) Structured programming Creating the final module 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)

3 Pseudocode Standard Control Structures NameStructureExample Example of translating in assembler language if-thenif then if AL = 0 then NEXT CMP AL, 0 JZ NEXT RET NEXT:... if-then - elseif then else if AL  0 then NEXT else SUM CMP AL, 0 JNZ NEXT SUM:... RET NEXT:... repeat - untilrepeat until initialize counter to 100 repeat GETDATA PROCDATA decrement counter until counter = 0 MOV BX,100 AGAIN:CALL GETDATA CALL PROCDATA DEC BX JNZ AGAIN while - dowhile do end-while while char  ‘A’ do end-while WHILE:CMP AL, ‘A’ JZ NEXT... JMP WHILE NEXT:...

4 Pseudocode Standard Control Structures NameStructureExample Example of translating in assembler language casecase of :... : 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:... casecase of :... : otherwise : 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:...

5 Writing a Software Driver QUADPROC 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 QUADENDP ;Program TESTQUAD.ASM: Software ; driver for QUAD procedure.MODEL SMALL.DATA XDB 0,1,10,100 YDW 6, 9, 486, 49806 PASSDB ’Procedure passes.’, 0Dh, 0Ah, ’$’ FAILDB ’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 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 END

6 .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 String Operations ;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 CMDSDB ’EXAM’, ’DUMP’, ’RUN ’ DB ’STOP’, ’LOAD’ JMPTBDW DOEXAM, DODUMP DW DORUN, DOSTOP DW DOLOAD CMBUFDB 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 LOOP CHECK ;continue with pass DEC DX ;dec pass counter JNZ DOPASS ;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 Pointer to list Content of a register Address of first byte in node 2: 2 bytes if near, 4 bytes if far. First byte in node 2 A node, including data and pointer to next node Example: SI=1200 1200 4502 1400 2044 Inserting an element on first position: SI=3100 1200 4502 1400 2044 7506 deleting an element from list Inserting an element Adding an element 5204

10 Data structures Stacks Stack = LIFO (Last Input, First Output) list.... 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 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 initial SP SP after PUSH SP after CALL 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 00 46 21 14 SP after PUSH SP before RET 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA SP after POPs SP after add 2 BP points here ?? 12 00 34 00 21 14 A dangerous example ( not recommended): (an interrupt here destroys the stack)

11 Data structures Stacks... 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 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 initial SP SP after PUSH SP after CALL 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 00 46 21 14SP before RET 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA BP+4 BP+2 BP points here ?? 12 00 34 00 21 14 A better way to do same job: (SP not modified. INT uses stack below 0FFA)

12 Data structures 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:... 1000 0FFF 0FFE 0FFD 0FFC 0FFB 0FFA ?? 12 00 34 00 21 14 SI BP (reader) SP (writer) DI (an interrupt in this routine destroys the stack also) 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)

13 Programming with DOS and BIOS Function Calls 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 Microprocessor Systems Based on "An Introduction to the Intel Family of Microprocessors

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

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. DOS INT 21, function 08H: Console Input without Echo Specification: similar to function 01 but no echo on video display. 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. BIOS INT 16, function 00H: Read keyboard Input Specification: similar to INT21 function 01 but if 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. 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). Control characters perform their specific action (0DH = Carriage Return, 0AH = Line Feed, 08H = Backspace, etc.). 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). BIOS INT 10, function 0FH: Read Current Video Mode Specification: returns video mode of the display. 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) Input: AH = 09 (function code), DX = The offset of the first character in the string. Input: AH = 00 (function code), AL = The desired video mode. Input: AH = 0F (function code) Output: AL = The current video mode. 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. 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 BIOS INT 10, function 08H: Read Character/Attribute from Screen Input: AH = 08 (function code), BH = display page (0) Input: AH = 0A (function code), AL = ASCII code, BH = page number, CX = repeat value. Input: AH = 09 (function code), AL = ASCII code, BH = page number, BL = character’s attribute, CX = repeat value. 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!


Download ppt "An Introduction to Programming the 80x86 Objectives: Breaking down a large program into small tasks How to write a software driver Reading character strings."

Similar presentations


Ads by Google