Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processing String Data and Binary Data (continue)

Similar presentations


Presentation on theme: "Processing String Data and Binary Data (continue)"— Presentation transcript:

1 Processing String Data and Binary Data (continue)

2 Code for last practice exercise
DATASEG SEGMENT PARA 'Data' ; Please insert your data declaration here DES_STR DB 50 DUP ('$') ParaList label byte maxlen DB 50 actlen DB ? SRC_STR DB 50 DUP ('$') DATASEG ENDS

3 Code for last practice exercise
; Code for reading input from keyboard MOV AH, 0AH LEA DX, ParaList INT 21H ; Code for reversing the string LEA SI, SRC_STR LEA DI, DES_STR MOV CH,0 MOV CL, ACTLEN DEC CX ADD DI, CX INC CX L10: LODSB MOV [DI], AL DEC DI LOOP L10

4 CMPSB, CMPSW Compare String Instruction
Compare bytes: ES:[DI] from DS:[SI]. DS:[SI] - ES:[DI] set flags according to result: OF, SF, ZF, AF, PF, CF if DF = 0 then SI = SI + 1 DI = DI + 1 else SI = SI - 1 DI = DI - 1

5 Example STRING1 DB ‘COMPUTER’ STRING2 DB ‘COMPUTER’ MOV CX, 8
LEA DI, STRING2 LEA SI, STRING1 REPE CMPSB JNE exit exit: …

6 REPE (repeat if equal) Repeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 1 (result is Equal), maximum CX times. check_cx: if CX <> 0 then do following chain instruction CX = CX - 1 if ZF = 1 then: go back to check_cx else exit from REPE cycle

7 SCASB: SCAN STRING INSTRUCTION
Scan a string for a specified value. Continue to Compare bytes: AL from ES:[DI] while the comparison is not equal or until CX is 0 (Similar to indexOf in java or strstr in C) ES:[DI] - AL set flags according to result: OF, SF, ZF, AF, PF, CF if DF = 0 then DI = DI + 1 else DI = DI - 1

8 Example: STRING1 DB ‘COMPUTER’ … MOV AL, ‘r’ MOV CX, 8 LEA DI, STRING1
REPNE SCASB

9 REPNE Repeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 0 (result is Not Equal), maximum CX times. check_cx: if CX <> 0 then do following chain instruction CX = CX - 1 if ZF = 0 then: go back to check_cx else exit from REPNE cycle

10 Replicating a pattern PATTERN DB ‘|----|’ RESULT DB 42 DUP (‘ ‘)
MOV CX, 21 LEA DI, RESULT LEA SI, PATTERN REP MOVSW

11 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 move.asm

12 page 60,132 TITLE MovePractice Move ; STACK SEGMENT PARA STACK 'Stack' DW 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

13 Practice 4. Modify your code so that:
- Define: BUS_TITLE DB ‘COMPUTER WIZARDS’ - WORK_SPACE DB 16 DUP(20H) - Move BUS_TITLE to WORK_SPACE from left to right (CLD) - MOV BUS_TITLE to WORK_SPACE from right to left (STD) - COMPARE BUS_TITLE with WORK_SPACE, print “equal” if equal, print “not equal” if not 5. Compile and run

14 Code ; Data segment declaration EQUALPROMPT DB 'Equal', '$'
NOTEQUALPROMPT DB 'Not Equal', '$' BUS_TITLE DB 'COMPUTER WIZARDS','$' WORK_SPACE DB 17 DUP(20H)

15 Code LEA SI, BUS_TITLE LEA DI, WORK_SPACE MOV CX, 17 REP MOVSB
; Moving from left to right CLD LEA SI, BUS_TITLE LEA DI, WORK_SPACE MOV CX, 17 REP MOVSB ; Moving from right to left STD LEA SI, BUS_TITLE+16 LEA DI, WORK_SPACE+16

16 Code ; Print equal if equal, print not equal if not MOV CX, 17
LEA DI, BUS_TITLE LEA SI, WORK_SPACE REPE CMPSB JE EQUALPRINT MOV AH, 09H LEA DX, NOTEQUALPROMPT INT 21H JMP exit EQUALPRINT: LEA DX, EQUALPROMPT

17 Binary Data ADC CBW NEG SBB

18 ADC Add with Carry. Algorithm: operand1 = operand1 + operand2 + CF

19 Example STC ; set CF =1 MOV AL, 5 ; AL = 5 ADC AL, 1 ; AL =?

20 CBW Convert byte into word. Algorithm: if high bit of AL = 1 then:
AH = 255 (0FFh) else AH = 0

21 Example MOV AX, 0 ; AH = 0, AL = 0 MOV AL, -5 ; AX = 000FBh (251)
CBW ; AX = 0FFFBh (-5)

22 NEG Negate. Makes operand negative (two's complement). Algorithm: Invert all bits of the operand Add 1 to inverted operand

23 Example MOV AL, 5 ; AL = 05h NEG AL ; AL = 0FBh (-5)
NEG AL ; AL = 05h (5)

24 SBB Subtract with Borrow. Algorithm: operand1 = operand1 - operand2 - CF

25 Example STC ; Set CF=1 MOV AL, 5 SBB AL, 3 AL =?

26 Distinguish between a carry and an overflow
Arithmetic carry (CF) An arithmetic operation transfers the resulting sign bit (0 or 1) to CF. When the carry occurs with unsigned data, the result is invalid. When carry occurs with signed data, the result is valid

27 Distinguish between a carry and an overflow
Arithmetic overflow (OF) Example: unsigned signed decimal (1) (invalid) (invalid) CF = 1 OF = 1

28 Note about DIV/ IDIV If divisor is a byte, a value must be greater than the left byte of the dividend (AH) If divisor is a word, its value must be greater than left word of the dividend (DX)

29 Define doubleword using words
Assume, we have H, we can define this as: DDVAR DW 0147H DW 1329H

30 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 add.asm

31 page 60,132 TITLE AddPractice Add ; STACK SEGMENT PARA STACK 'Stack' DW 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

32 Practice 4. Define data: BIN1 DW 0147H DW 1329H BIN2 DW 02B3H DW 0241H
SUM DW DW Write a program to add the doublewords beginning at BIN1 and BIN2 and store the result to doubleword beginning at SUM SUM= 156A03FAH


Download ppt "Processing String Data and Binary Data (continue)"

Similar presentations


Ads by Google