Download JNZ NEXT - WSU EECS

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
EE314
Microprocessor Systems
Chapter 6
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" by James L. Antonakos
6.2 Tackling a Large Programming Assignment
The assignment in the
form of specification
Breaking down the
Structured
programming program into modules
Testing the modules
Creating the final
module
•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)
• each module is defined trough it’s own
specification
• using a driver: supplies the subroutine with
sample input, appeals the subroutine and verifies
the routine’s output for correctness)
• 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)
Pseudocode Standard Control Structures
Name
Structure
Example
if-then
if <condition>
then <action>
if AL = 0
then NEXT
Example of translating in
assembler language
NEXT:
if-then - else if <condition>
then <action1>
else <action2>
if AL  0
then NEXT
else SUM
SUM:
NEXT:
repeat - until repeat
<statements>
until <condition>
while - do
initialize counter to 100
repeat
GETDATA
PROCDATA
decrement counter
until counter = 0
while <condition> do while char  ‘A’ do
<statements>
<statements>
end-while
end-while
CMP AL, 0
JZ NEXT
RET
...
CMP AL, 0
JNZ NEXT
...
RET
...
MOV BX,100
AGAIN: CALL GETDATA
CALL PROCDATA
DEC BX
JNZ AGAIN
WHILE: CMP AL, ‘A’
JZ NEXT
...
JMP WHILE
NEXT:
...
Pseudocode Standard Control Structures
Name
Structure
case case <item> of
<item1> : <statement 1>
<item2> : <statement 1>
...
<itemX> : <statement X>
Example
Example of translating in
assembler language
case selvalue of
0 : clear counter
1 : increment counter
2 : decrement counter
C1:
C2:
NEXT:
case case <item> of
<item1> : <statement 1>
<item2> : <statement 1>
...
<itemX> : <statement X>
otherwise : <other st.>
case selvalue of
1 : increment counter
2 : decrement counter
otherwise : clear counter
CMP AL, 0 ;is it 0?
JNZ C1
MOV BL, 0 ;clear counter
JMP NEXT
CMP AL, 1 ;is it 1?
JNZ C2
INC BL
;inc counter
JMP NEXT
CMP AL, 2 ;is it 2?
JNZ NEXT ;no matches
DEC BL
;dec 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:
...
6.3 Writing a Software Driver
Y  5X 2  2X 6
;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, ’$’
BAD:
SEND:
QUAD
.CODE
.STARTUP
MOV AL, X[0]
CALL QUAD
CMP AX, Y[0]
JNZ BAD
MOV AL, X[1]
CALL QUAD
CMP AX, Y[2]
JNZ BAD
MOV AL, X[2]
CALL QUAD
CMP AX, Y[4]
JNZ BAD
;load first test value
;compute result
;look for match
;load second test value
;compute result
;look for match
;load third test value
;compute result
;look for match
QUAD
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
LEA DX, FAIL
MOV AH, 9
INT 21h
.EXIT
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
ENDP
END
6.6 String Operations
.CODE
;Program COMREC.ASM:
;A command recognizer.
.STARTUP
;5 available commands,
CALL COMREC
;each 4 character wide,
.EXIT
;placed in .DATA, one after other,
;beginning at address CMDS.
COMREC PROC FAR
;5 near routine addresses,
LEA BX,CMDS ;point to command table
;one for each valid command,
MOV DI,0
;init index within JMPTB
;placed in .DATA, starting at
MOV CX,5
;init loop counter for 5 cmds.
;address JMPTB.
NXTC: PUSH CX
;save loop counter
;The program jumps to the routine
MOV CX,4
;init loop cnt. for 4 chrs. in cmd.
;who’s name was recognized.
MOV SI,0
;The actual command is 4 byte wide, CHK: MOV AL,[BX+SI] ;get a table character
;stored in .DATA at address CMBUF.
CMP CMBUF[SI],AL;compare it with command
;If no valid command is recognized,
JNZ NOM
;program jumps to label CMER.
INC SI
;point to next character
LOOP CHK
;continue comparison
.MODEL SMALL
POP CX
;match found, fix stack
.DATA
JMP JMPTB[DI] ;jump to command routine
CMDS
DB ’EXAM’, ’DUMP’, ’RUN ’ NOM: POP CX
;get loop counter back
DB ’STOP’, ’LOAD’
ADD BX,4
;point to next command text
JMPTB DW DOEXAM, DODUMP
ADD DI,2
;and next routine address
DW DORUN, DOSTOP
LOOP NXTC
;go check next command
DW DOLOAD
CMER: RET
CMBUF DB 4 DUP (?)
COMREC ENDP
6.6 String Operations
;Program LASTNAME.ASM: Search database for
;last name of each person and displays it.
LASTN PROC NEAR
;Record: ends with ASCII character “0Dh”.
mov AL,20H ;character to find
;Last Name begins after first “20h” in record
mov CX,64 ;max length of record
;ends before first ”,” in record.
cld
;set auto increment
;Database begins at adr. DBASE, ends at first “0”
repnz scasb ;skip to beg. of last name
.MODEL SMALL
DSPN:mov DL,[DI] ;load string character
.DATA
cmp DL,','
;past end of last name?
DBASE DB 'Dave Guza, MPC, 2389, B26',0Dh
jz FEND
;jump if so
DB 'James Antonakos, EET, 2356, B20',0Dh
mov AH,2 ;displays character
DB 'Mike Fisher, RWA, 2376, A19',0Dh
int 21H
;DOS call
DB 'Jennifer Indigo, CS/AT, 2676, A7',0Dh
inc DI
;next character
DB 'William Robinson, LIS, 2300, J2',0Dh
jmp DSPN ;and repeat
DB 'Michele Tanner, ILY, 2143, B45',0Dh
FEND:mov DL,0DH ;output a CR
DB 0 ;end of database
mov AH,2
.CODE
int 21H
.STARTUP
mov DL,0AH ;output a line feed
mov AX, seg DBASE
mov AH,2
mov DS,AX ;load address of data segment
int 21H
mov ES,AX ;set up extra segment
mov AL,0DH ;character to find
lea DI,DBASE ;set up pointer to database
mov CX,64 ;max length of record
NEXT:
repnz scasb ;skip to end of record
call LASTN ;display next person's last name
ret
cmp byte ptr [DI],0 ;end of DBASE reached?
LASTNAME ENDP
jnz NEXT
;repeat if no match
END
.EXIT
6.7 Sorting a string
;Sorts “NV” bytes at adr.“VAL”, increasing order
.MODEL SMALL
.DATA
first step
VAL DB
7,10,6,3,9
7, 10, 6, 3, 9
NV DW
5
.CODE
after 1st step
.STARTUP
7, 10, 6, 3, 9
CALL SORT
.EXIT
after 2nd step
SORT PROC FAR
7, 6, 10, 3, 9
MOV DX,NV ;get number of data items
DEC DX
;subtract one to start
DX=4
after 3rd step
DOPASS: MOV CX,DX ;init loop counter
CX=4
7, 6, 3, 10, 9
MOV SI,0
;init data pointer
CHECK: MOV AL,VAL[SI];get first val
AL=7
AL=10
AL=10
AL=10
CMP VAL[SI+1],AL;cmp with 2nd val
cmp 10
cmp 6
cmp 3
cmp 9
JNC NOSWAP
MOV BL,VAL[SI+1] ;swap elements
no swap swap 10,6 swap 10,3 swap 10,9
MOV VAL[SI+1],AL
MOV VAL[SI],BL
NOSWAP: INC SI
;point to next val
“10”
“10”
“10”
“10”
LOOP CHECK ;continue with pass
CX=3
CX=2
CX=1
CX=0
DEC DX
;dec pass counter
DX=3
JNZ DOPASS ;until finished
after 4th step
RET
7, 6, 3, 9 , 10
Val max. already al last position
SORT ENDP
next loop goes only on first 4 values (3 steps)
END
6.11 Data structures
Linked lists
Content of a register
Pointer to list
Address of first byte in node 2: 2 bytes if near, 4 bytes if far.
Data in node 1 Pointer to node 2
A node, including data
and pointer to next node
Data in node 2 Pointer to node 3
Data in node 3 Pointer to node 4
First byte in node 2
Example:
SI=1200
15,5F,34,7A
4502
15,5F,34,7A
7506
FD,48,55,80
2044
E3,38,AD,00
2044
15,5F,34,7A
1200
1200
FD,48,55,80
1400
4502
4502
E3,38,AD,00
2044
75,60,11,F5
Adding an element
75,60,11,F5
0000
2044
4502
7506
deleting an element from list
1400
1400
2044
Inserting an
Data in node 4 0=null=nil (end of list)
element on
first position: SI=3100
15,5F,34,7A 1200
Inserting an element
5204
75,60,11,F5
5204
0000
6.11 Data structures
Stacks
Stack = LIFO (Last Input, First Output) list.
A dangerous
... example ( not recommended):
2114
PUSH 12h
PUSH 34h
CALL STAD
POP AX
POP BX
…
STAD PROC NEAR
(an interrupt here
destroys the stack)
MOV BP,SP
ADD SP,2
POP AX
POP BX
ADD AX,BX
MOV BX,0
ADC BX,BX
NOM: PUSH BX
PUSH AX
MOV SP,BP
RET
;prepare first operand.
;prepare second operand
;add operands.
;peak lower word of result
;peak lower word of result
;add the two words on stack
;before return address.
;Returns the double word
;result in the same place.
;save SP pointing RET address
;SP points second operand
;load second operand from stack
;load second operand from stack
;add operands in AX
;load 0 into BX
;add CF to BX
;save higher word of result
;save lower word of result
;restore SP for RET
initial SP
1000
0FFF
0FFE
0FFD
SP after PUSH 0FFC
0FFB
SP after CALL 0FFA
SP after POPs 1000
0FFF
0FFE
0FFD
SP after add 2 0FFC
0FFB
BP points here 0FFA
1000
0FFF
0FFE
0FFD
SP after PUSH 0FFC
0FFB
SP before RET 0FFA
??
12
00
34
00
21
14
??
12
00
34
00
21
14
??
00
00
00
46
21
14
6.11 Data structures
Stacks
A better way to do same job:
2114
...
PUSH 12h
PUSH 34h
CALL STAD
POP AX
POP BX
…
STAD PROC NEAR
;prepare first operand.
;prepare second operand
;add operands.
;peak lower word of result
;peak lower word of result
;add the two words on stack
;before return address.
(SP not modified.
;Returns the double word
INT uses stack below
;result in the same place.
0FFA) 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
initial SP
1000
0FFF
0FFE
0FFD
SP after PUSH 0FFC
0FFB
SP after CALL 0FFA
1000
0FFF
BP+4
0FFE
0FFD
BP+2
0FFC
0FFB
BP points here 0FFA
1000
0FFF
0FFE
0FFD
0FFC
0FFB
SP before RET 0FFA
??
12
00
34
00
21
14
??
12
00
34
00
21
14
??
00
00
00
46
21
14
6.11 Data structures
Queues
SI
Queue = FIFO (First Input, First Output) list.
BP (reader)
1000
0FFF
0FFE
0FFD
0FFC
0FFB
0FFA
??
12
00
34
00
21
14
SI content specify the begin address of queue (highest)
DI content specify the end address of queue (lowest) SP (writer)
SP pointer to write into queue (each PUSH
automatically decrements SP)
DI
SI pointer to read from queue (BP has to be
decrement explicitly) - difference
between PUSH and MOV instructions
should be observed)
The routine should verify other
(an interrupt in this routine
destroys the stack also)
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:
...
two conditions:
- BP not pass SP (try to read
unwritten data)
- SP not pass BP (try to write
over data not yet red)