Processing String Data and Binary Data (continue)
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
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
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
Example STRING1 DB ‘COMPUTER’ STRING2 DB ‘COMPUTER’ MOV CX, 8 LEA DI, STRING2 LEA SI, STRING1 REPE CMPSB JNE exit exit: …
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
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
Example: STRING1 DB ‘COMPUTER’ … MOV AL, ‘r’ MOV CX, 8 LEA DI, STRING1 REPNE SCASB
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
Replicating a pattern PATTERN DB ‘|----|’ RESULT DB 42 DUP (‘ ‘) MOV CX, 21 LEA DI, RESULT LEA SI, PATTERN REP MOVSW
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
page 60,132 TITLE MovePractice Move ; --------------------------------------------- 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: 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
Code ; Data segment declaration EQUALPROMPT DB 'Equal', '$' NOTEQUALPROMPT DB 'Not Equal', '$' BUS_TITLE DB 'COMPUTER WIZARDS','$' WORK_SPACE DB 17 DUP(20H)
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
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
Binary Data ADC CBW NEG SBB
ADC Add with Carry. Algorithm: operand1 = operand1 + operand2 + CF
Example STC ; set CF =1 MOV AL, 5 ; AL = 5 ADC AL, 1 ; AL =?
CBW Convert byte into word. Algorithm: if high bit of AL = 1 then: AH = 255 (0FFh) else AH = 0
Example MOV AX, 0 ; AH = 0, AL = 0 MOV AL, -5 ; AX = 000FBh (251) CBW ; AX = 0FFFBh (-5)
NEG Negate. Makes operand negative (two's complement). Algorithm: Invert all bits of the operand Add 1 to inverted operand
Example MOV AL, 5 ; AL = 05h NEG AL ; AL = 0FBh (-5) NEG AL ; AL = 05h (5)
SBB Subtract with Borrow. Algorithm: operand1 = operand1 - operand2 - CF
Example STC ; Set CF=1 MOV AL, 5 SBB AL, 3 AL =?
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
Distinguish between a carry and an overflow Arithmetic overflow (OF) Example: unsigned signed decimal 11110110 246 -10 + 10001001 37 -119 (1)01111111 127 (invalid) -129 (invalid) CF = 1 OF = 1
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)
Define doubleword using words Assume, we have 13290147H, we can define this as: DDVAR DW 0147H DW 1329H
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
page 60,132 TITLE AddPractice Add ; --------------------------------------------- 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. Define data: BIN1 DW 0147H DW 1329H BIN2 DW 02B3H DW 0241H SUM DW 0 DW 0 Write a program to add the doublewords beginning at BIN1 and BIN2 and store the result to doubleword beginning at SUM SUM= 156A03FAH