Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining and processing tables. Tables Defining tables Searching Sorting.

Similar presentations


Presentation on theme: "Defining and processing tables. Tables Defining tables Searching Sorting."— Presentation transcript:

1 Defining and processing tables

2 Tables Defining tables Searching Sorting

3 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

4 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

5 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)

6 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

7 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

8 Searching Equal: MOV AH, 09H LEA DX, prompt INT 21H

9 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

10 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

11 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:","$"

12 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

13 Project 5 clearscreen proc mov ah,06h mov al,00h mov bh,0x9fh mov cx,0000h mov dx,184fh int 10h ret clearscreen endp

14 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

15 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

16 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

17 Project 5 compnames proc mov cx,50 lea si,input lea di,input2 repe cmpsb jg gt call adddollar

18 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

19 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

20 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

21 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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1649306/7/slides/slide_20.jpg", "name": "Sorting a table Arrange data in a table in a certain order, such as ascending or descending.", "description": "Sorting algorithm (bubble sort) for (i=0; i

22 Sorting a table-Code LEN EQU 3 LEN_ELEMENT EQU 5 TABLE DB 6,7,1 ENDADDR DW ? SWAPPED DB 00

23 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

24 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

25 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

26 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)

27 Program break down MOV SWAPPED, 00 LEA SI, TABLE inside: MOV DI, SI INC DI MOV AX, DI MOV BX, SI CMPSB JBE isTheEnd CALL EXCHANGE ; SWAPPED = 0 ;SI contains the offset of the table SI=0 DI = 1 AX= 1BX= 0 (TABLE[0] = 6) < (TABLE[1] = 7) Jump to isTheEND SI = 1DI = 2

28 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

29 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 = 2BX = 1 (TABLE[1] = 7) > (TABLE[2] = 1) CALL EXCHANGE SI = 2DI = 3

30 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=2SI = SI-1 =2-1=1 BL = Table[2]=1BH = Table[1] = 7 Table[2] = 7Table[1] = 1 SWAPPED = 1

31 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

32 Program break down Original array: 6 7 1 First pass: 6 1 7 Similarly, second pass would produce: 1 6 7

33 Lab/Practice 1. Open your browser and open this page: C:\emu8086\documentation\8086_instruction_se t.html And C:\emu8086\documentation\8086_and_dos_inter rupts.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

34 page 60,132 TITLESortPractice ; --------------------------------------------- STACKSEGMENT PARA STACK 'Stack' DW 32 DUP(0) STACKENDS ; ---------------------------------------------- DATASEGSEGMENT PARA 'Data' ; Please insert your data declaration here DATASEGENDS CODESEG SEGMENT PARA 'Code' MAINPROC 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

35 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

36 Lab/Practice 1. Open your browser and open this page: C:\emu8086\documentation\8086_instruction_se t.html And C:\emu8086\documentation\8086_and_dos_inter rupts.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

37 page 60,132 TITLEScanPractice ; --------------------------------------------- STACKSEGMENT PARA STACK 'Stack' DW 32 DUP(0) STACKENDS ; ---------------------------------------------- DATASEGSEGMENT PARA 'Data' ; Please insert your data declaration here DATASEGENDS CODESEG SEGMENT PARA 'Code' MAINPROC 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

38 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

39 Lab/Practice 1. Open your browser and open this page: C:\emu8086\documentation\8086_instruction_se t.html And C:\emu8086\documentation\8086_and_dos_inter rupts.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

40 page 60,132 TITLEReversePractice ; --------------------------------------------- STACKSEGMENT PARA STACK 'Stack' DW 32 DUP(0) STACKENDS ; ---------------------------------------------- DATASEGSEGMENT PARA 'Data' ; Please insert your data declaration here DATASEGENDS CODESEG SEGMENT PARA 'Code' MAINPROC 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

41 Practice 4. Modify your code so that: - Define: NAME1 with string COMPUTER TECH - NAME2 with 13 blank space a. Use LODSB to access each character in NAME1 from left to right b. Use STOSB to store each accessed character into NAME2 from right to left. (NAME2 will contain reverse string of NAME1) 5. Compile and run


Download ppt "Defining and processing tables. Tables Defining tables Searching Sorting."

Similar presentations


Ads by Google