defining and processing tables. tables defining tables searching sorting

41
Defining and processing tables

Upload: jaime-hayre

Post on 31-Mar-2015

246 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Defining and processing tables. Tables Defining tables Searching Sorting

Defining and processing tables

Page 2: Defining and processing tables. Tables Defining tables Searching Sorting

Tables

Defining tablesSearchingSorting

Page 3: 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 ’

Page 4: Defining and processing tables. Tables Defining tables Searching Sorting

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 tableE.g: WORD_TABLE DW 12 DUP(?)Type: 2 bytesLength: 12Size: 12*2 = 24 bytes

Page 5: Defining and processing tables. Tables Defining tables Searching Sorting

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)

Page 6: Defining and processing tables. Tables Defining tables Searching Sorting

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 02LENGTH_DESCRIPTION EQU 08

Page 7: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 8: Defining and processing tables. Tables Defining tables Searching Sorting

Searching

Equal:

MOV AH, 09H

LEA DX, prompt

INT 21H

Page 9: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 10: Defining and processing tables. Tables Defining tables Searching Sorting

Example

MOV CX, 03 LEA DI, MONTH_TBL AGAIN: MOV CX,LENGTH_INDEX

LEA SI, MONTH_INREPE CMPSBJE EqualExitJB ExitADD DI, CXADD DI, LENGTH_DESCRIPTIONJMP AGAIN

EqualExit:

MOV AH, 09H LEA DX, prompt INT 21H

Page 11: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 12: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 13: Defining and processing tables. Tables Defining tables Searching Sorting

Project 5

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

Page 14: Defining and processing tables. Tables Defining tables Searching Sorting

Project 5getnames 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

Page 15: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 16: Defining and processing tables. Tables Defining tables Searching Sorting

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 retgetnames endp

Page 17: Defining and processing tables. Tables Defining tables Searching Sorting

Project 5

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

Page 18: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 19: Defining and processing tables. Tables Defining tables Searching Sorting

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 retcompnames endp

Page 20: Defining and processing tables. Tables Defining tables Searching Sorting

Project 5adddollar 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 retadddollar endp

end main

Page 21: Defining and processing tables. Tables Defining tables Searching Sorting

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;

} } }

Page 22: Defining and processing tables. Tables Defining tables Searching Sorting

Sorting a table-Code

LEN EQU 3

LEN_ELEMENT EQU 5

TABLE DB 6,7,1

ENDADDR DW ?

SWAPPED DB 00

Page 23: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 24: Defining and processing tables. Tables Defining tables Searching Sorting

Sorting a table - CodeOUTSIDE:

MOV SWAPPED, 00 LEA SI, TABLE inside: MOV DI, SI

INC DI MOV AX, DIMOV BX, SI

CMPSB JBE isTheEnd

CALL EXCHANGE isTheEnd:

MOV SI, AX CMP SI, ENDADDR JB inside

CMP SWAPPED, 00 JNZ outside

Page 25: Defining and processing tables. Tables Defining tables Searching Sorting

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 RETEXCHANGE ENDP

Page 26: Defining and processing tables. Tables Defining tables Searching Sorting

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)

Page 27: Defining and processing tables. Tables Defining tables Searching Sorting

Program break down

MOV SWAPPED, 00LEA SI, TABLE inside: MOV DI, SI

INC DI MOV AX, DIMOV BX, SICMPSBJBE isTheEndCALL EXCHANGE

; SWAPPED = 0

;SI contains the offset of the tableSI=0

DI = 1

AX= 1 BX= 0

(TABLE[0] = 6) < (TABLE[1] = 7)

Jump to isTheEND

SI = 1 DI = 2

Page 28: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 29: Defining and processing tables. Tables Defining tables Searching Sorting

Program break down

inside: MOV DI, SI

INC DI MOV AX, DIMOV BX, SICMPSBJBE isTheEndCALL EXCHANGE

SI = 1

DI = 2

AX = 2 BX = 1

(TABLE[1] = 7) > (TABLE[2] = 1)

CALL EXCHANGE

SI = 2 DI = 3

Page 30: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 31: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 32: Defining and processing tables. Tables Defining tables Searching Sorting

Program break down

Original array: 6 7 1

First pass: 6 1 7

Similarly, second pass would produce:

1 6 7

Page 33: Defining and processing tables. Tables Defining tables Searching Sorting

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 34: Defining and processing tables. Tables Defining tables Searching Sorting

page 60,132TITLE SortPractice; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'; Please insert your data declaration hereDATASEG ENDS

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 21HMAIN ENDP

CODESEG ENDSEND MAIN ;End of program

Page 35: Defining and processing tables. Tables Defining tables Searching Sorting

Practice4. Modify your code so that:a. You declare a table with 3 elements, each element has 5 charactersTABLE 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

Page 36: Defining and processing tables. Tables Defining tables Searching Sorting

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 37: Defining and processing tables. Tables Defining tables Searching Sorting

page 60,132TITLE ScanPractice; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'; Please insert your data declaration hereDATASEG ENDS

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 21HMAIN ENDP

CODESEG ENDSEND MAIN ;End of program

Page 38: Defining and processing tables. Tables Defining tables Searching Sorting

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

Page 39: Defining and processing tables. Tables Defining tables Searching Sorting

Lab/Practice

1. Open your browser and open this page:C:\emu8086\documentation\

8086_instruction_set.htmlAndC:\emu8086\documentation\

8086_and_dos_interrupts.html2. Open your emu8086 software3. Cut and paste (or type) the following code (as

shown in the next page) and save reverse.asm

Page 40: Defining and processing tables. Tables Defining tables Searching Sorting

page 60,132TITLE ReversePractice; ---------------------------------------------STACK SEGMENT PARA STACK 'Stack'

DW 32 DUP(0)STACK ENDS; ----------------------------------------------DATASEG SEGMENT PARA 'Data'; Please insert your data declaration hereDATASEG ENDS

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 21HMAIN ENDP

CODESEG ENDSEND MAIN ;End of program

Page 41: Defining and processing tables. Tables Defining tables Searching Sorting

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