410 likes | 565 Views
Defining and processing tables. Tables. Defining tables Searching Sorting. Defining tables. MONTH_TBL DB ‘Jan’,’Feb’,Mar’,…,’Dec’ NUMBER_TBL DB 205,208,209,…,210 STOCK_TBL DB 12,’Computer’, 14,’Paper ’, 17,’Diskettes ’. TYPE,LENGTH and SIZE.
E N D
Tables • Defining tables • Searching • Sorting
Defining tables • MONTH_TBL DB ‘Jan’,’Feb’,Mar’,…,’Dec’ • NUMBER_TBL DB 205,208,209,…,210 • STOCK_TBL DB 12,’Computer’, 14,’Paper ’, 17,’Diskettes ’
TYPE,LENGTH and SIZE • TYPE operator is used to determine the definition. • LENGTH operator is used to determine the number of elements in the table • SIZE operator is used to determine the number of bytes used for this table E.g: WORD_TABLE DW 12 DUP(?) Type: 2 bytes Length: 12 Size: 12*2 = 24 bytes
Example RAIN_TBLE DW 12 DUP(?) MOV AX, TYPE RAIN_TBL MOV BX, LENGTH RAIN_TBL MOV CX, SIZE RAIN_TBL (These are not available in EMU8086)
SEARCHING A TABLE Example: Give a table: MONTH_TBL DB ’01’,’January ’ DB ’02’, ’February’ DB ’03’, ‘March ‘ Find the entry of the table that equals to MONTH_IN DB ’02’,’February’ LENGTH_INDEX EQU 02 LENGTH_DESCRIPTION EQU 08
Searching table (continue) MOV CX, 03 LEA SI, MONTH_TBL AGAIN: MOV AL, MONTH_IN CMP AL,[SI] JNE NOTEQUAL MOV AL, MONTH_IN+1 CMP AL,[SI+1] JE EQUAL NOTEQUAL: JB Exit Add SI, LENGTH_INDEX Add SI, LENGTH_DESCRIPTION Loop Again
Searching Equal: MOV AH, 09H LEA DX, prompt INT 21H
Searching tables with string comparisons Example: Given MONTH_TBL DB ’001’,’January ’ DB ’002’, ’February’ DB ’003’, ‘March ‘ Find the entry of the table that equals to MONTH_IN DB ’002’ LENGTH_INDEX DB 03 LENGTH_DESCRIPTION DB 08
Example MOV CX, 03 LEA DI, MONTH_TBL AGAIN: MOV CX,LENGTH_INDEX LEA SI, MONTH_IN REPE CMPSB JE EqualExit JB Exit ADD DI, CX ADD DI, LENGTH_DESCRIPTION JMP AGAIN EqualExit: MOV AH, 09H LEA DX, prompt INT 21H
Project 5 .model small, .stack .data para_list label byte max_len db 50 act_len db ? input db 50 dup(' ') para_list2 label byte max_len2 db 50 act_len2 db ? input2 db 50 dup(' ') output1 db "Enter a name:","$" output2 db "Enter another name:","$"
Project 5 .code main proc mov ax,@data mov ds,ax mov es,ax start: call clearscreen call getnames call clearscreen call compnames mov ax, 4c00h int 21h main endp
Project 5 clearscreen proc mov ah,06h mov al,00h mov bh,0x9fh mov cx,0000h mov dx,184fh int 10h ret clearscreen endp
Project 5 getnames proc mov ah,02h mov bh,00 mov dx,0000h int 10h ;display the input string mov ah,09 lea dx,output1 int 21h ;put the cursor in the top left area mov ah,02h mov bh,00 mov dx,0100h int 10h
Project 5 ;keyboard input mov ah,0ah lea dx,para_list int 21h ;put the cursor just up and left of the screen mov ah,02h mov bh,00 mov dx,0200h int 10h ;display the input string mov ah,09 lea dx,output2 int 21h
Project 5 ;put the cursor in the top left area mov ah,02h mov bh,00 mov dx,0300h int 10h ;keyboard input mov ah,0ah lea dx,para_list2 int 21h ret getnames endp
Project 5 compnames proc mov cx,50 lea si,input lea di,input2 repe cmpsb jg gt call adddollar
Project 5 mov ah,02h mov bh,00 mov dx,0b20h int 10h ;display the input string mov ah,09 lea dx,input int 21h mov ah,02h mov bh,00 mov dx,0c20h int 10h ;display the input2 string mov ah,09 lea dx,input2 int 21h ret
Project 5 gt: call adddollar mov ah,02h mov bh,00 mov dx,0b20h Int 10h ;display the input2 string mov ah,09 lea dx,input2 int 21h mov ah,02h mov bh,00 mov dx,0c20h int 10h ;display the input string mov ah,09 lea dx,input int 21h ret compnames endp
Project 5 adddollar proc ;add $ to input mov al,act_len ;put the length in al mov ah,00 ;put ah to 0: ax now contains act_len mov si,ax ;si now contains act_len mov input[si],"$" ;add a '$' to the first name mov al,act_len2 ;ax contains the length of the second name mov si,ax ;now si does mov input2[si],"$" ;add a '$' to the second name ret adddollar endp end main
Sorting a table • Arrange data in a table in a certain order, such as ascending or descending. • Sorting algorithm (bubble sort)for (i=0; i<n-1; i++) { for (j=0; j<n-1-i; j++) { if (a[j+1] < a[j]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } }
Sorting a table-Code LEN EQU 3 LEN_ELEMENT EQU 5 TABLE DB 6,7,1 ENDADDR DW ? SWAPPED DB 00
Sorting a table - Code MOV AX, DATASEG ; MOV DS, AX ; MOV ES, AX LEA SI, TABLE MOV AX, SI ADD AX,2 MOV ENDADDR, AX
Sorting a table - Code OUTSIDE: MOV SWAPPED, 00 LEA SI, TABLE inside: MOV DI, SI INC DI MOV AX, DI MOV BX, SI CMPSB JBE isTheEnd CALL EXCHANGE isTheEnd: MOV SI, AX CMP SI, ENDADDR JB inside CMP SWAPPED, 00 JNZ outside
Sorting a table - code EXCHANGE PROC NEAR DEC DI DEC SI MOV BL,[DI] MOV BH,[SI] MOV [DI], BH MOV [SI], BL MOV SWAPPED, 01 RET EXCHANGE ENDP
Program break down These four instructions load the offset of table to SI. LEA SI, TABLE MOV AX, SI ADD AX,2 MOV ENDADDR, AX ENDADDR = offset of table +LENGTH of the table -1 = offset of table + 2 (in this specific example)
Program break down ; SWAPPED = 0 • MOV SWAPPED, 00 • LEA SI, TABLE • inside: • MOV DI, SI • INC DI • MOV AX, DI • MOV BX, SI • CMPSB • JBE isTheEnd • CALL EXCHANGE ;SI contains the offset of the table SI=0 DI = 1 AX= 1 BX= 0 (TABLE[0] = 6) < (TABLE[1] = 7) SI = 1 DI = 2 Jump to isTheEND
Program break down isTheEnd: MOV SI, AX CMP SI, ENDADDR JB inside CMP SWAPPED, 00 JNZ outside SI = AX = 1 (SI = 1) < (ENDADDR = 2) Jump to Inside
Program break down inside: MOV DI, SI INC DI MOV AX, DI MOV BX, SI CMPSB JBE isTheEnd CALL EXCHANGE SI = 1 DI = 2 AX = 2 BX = 1 (TABLE[1] = 7) > (TABLE[2] = 1) SI = 2 DI = 3 CALL EXCHANGE
Program break down EXCHANGE PROC NEAR DEC DI DEC SI MOV BL,[DI] MOV BH,[SI] MOV [DI], BH MOV [SI], BL MOV SWAPPED, 01 RET EXCHANGE ENDP DI = DI-1 =3-1=2 SI = SI-1 =2-1=1 BL = Table[2]=1 BH = Table[1] = 7 Table[2] = 7 Table[1] = 1 SWAPPED = 1
Program break down isTheEnd: MOV SI, AX CMP SI, ENDADDR JB inside CMP SWAPPED, 00 JNZ outside SI =AX = 2 (SI = 2) = ENDADDR (01 != 00) Jump to outside
Program break down Original array: 6 7 1 First pass: 6 1 7 Similarly, second pass would produce: 1 6 7
Lab/Practice 1. Open your browser and open this page: C:\emu8086\documentation\8086_instruction_set.html And C:\emu8086\documentation\8086_and_dos_interrupts.html 2. Open your emu8086 software 3. Cut and paste (or type) the following code (as shown in the next page) and save sort.asm
page 60,132 TITLE SortPractice ; --------------------------------------------- STACK SEGMENT PARA STACK 'Stack' DW 32 DUP(0) STACK ENDS ; ---------------------------------------------- DATASEG SEGMENT PARA 'Data' ; Please insert your data declaration here DATASEG ENDS CODESEG SEGMENT PARA 'Code' MAIN PROC FAR MOV AX, DATASEG MOV DS, AX MOV ES, AX ; Please insert your code here MOV AX,4C00H ;exit procedure INT 21H MAIN ENDP CODESEG ENDS END MAIN ;End of program
Practice 4. Modify your code so that: a. You declare a table with 3 elements, each element has 5 characters TABLE DB "CCCCC", DB "BBBBB", DB "AAAAA" b.Reuse the sorting code in previous lecture, change the followings: - re-compute ENDADDR - in inside loop, re-compute value of DI so that it points to the next element in the array - in inside loop, modify the code so that it compares two strings instead of comparing two integers - in exchange proc: modify the code so that it exchange byte by byte from left to right between DS:[SI] and ES:[DI] 5. Compile and run
Lab/Practice 1. Open your browser and open this page: C:\emu8086\documentation\8086_instruction_set.html And C:\emu8086\documentation\8086_and_dos_interrupts.html 2. Open your emu8086 software 3. Cut and paste (or type) the following code (as shown in the next page) and save s.asm
page 60,132 TITLE ScanPractice ; --------------------------------------------- STACK SEGMENT PARA STACK 'Stack' DW 32 DUP(0) STACK ENDS ; ---------------------------------------------- DATASEG SEGMENT PARA 'Data' ; Please insert your data declaration here DATASEG ENDS CODESEG SEGMENT PARA 'Code' MAIN PROC FAR MOV AX, DATASEG MOV DS, AX MOV ES, AX ; Please insert your code here MOV AX,4C00H ;exit procedure INT 21H MAIN ENDP CODESEG ENDS END MAIN ;End of program
Practice 4. Modify your code so that: - It gets a string from keyboard (use INT 21H, 0A). The string has maximum 25 characters - Code the instructions to scan this string and replace the first occurrence of a space with asterisk (*). 5. Compile and run
Lab/Practice 1. Open your browser and open this page: C:\emu8086\documentation\8086_instruction_set.html And C:\emu8086\documentation\8086_and_dos_interrupts.html 2. Open your emu8086 software 3. Cut and paste (or type) the following code (as shown in the next page) and save reverse.asm
page 60,132 TITLE ReversePractice ; --------------------------------------------- STACK SEGMENT PARA STACK 'Stack' DW 32 DUP(0) STACK ENDS ; ---------------------------------------------- DATASEG SEGMENT PARA 'Data' ; Please insert your data declaration here DATASEG ENDS CODESEG SEGMENT PARA 'Code' MAIN PROC FAR MOV AX, DATASEG MOV DS, AX MOV ES, AX ; Please insert your code here MOV AX,4C00H ;exit procedure INT 21H MAIN ENDP CODESEG ENDS END MAIN ;End of program
Practice 4. Modify your code so that: - Define: NAME1 with string ‘COMPUTER TECH’ - NAME2 with 13 blank space • Use LODSB to access each character in NAME1 from left to right • Use STOSB to store each accessed character into NAME2 from right to left. (NAME2 will contain reverse string of NAME1) 5. Compile and run