32
Processing String Data and Binary Data (continue)

Processing String Data and Binary Data (continue)

  • Upload
    leola

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

Processing String Data and Binary Data (continue). Code for last practice exercise. DATASEGSEGMENT 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 ('$') DATASEGENDS. - PowerPoint PPT Presentation

Citation preview

Page 1: Processing String Data and Binary Data (continue)

Processing String Data and Binary Data

(continue)

Page 2: 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

Page 3: Processing String Data and Binary Data (continue)

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

Page 4: Processing String Data and Binary Data (continue)

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

Page 5: Processing String Data and Binary Data (continue)

Example

STRING1 DB ‘COMPUTER’STRING2 DB ‘COMPUTER’

MOV CX, 8LEA DI, STRING2LEA SI, STRING1REPE CMPSBJNE exit

exit: …

Page 6: Processing String Data and Binary Data (continue)

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 else

exit from REPE cycle

Page 7: Processing String Data and Binary Data (continue)

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

Page 8: Processing String Data and Binary Data (continue)

Example:

STRING1 DB ‘COMPUTER’

MOV AL, ‘r’

MOV CX, 8

LEA DI, STRING1

REPNE SCASB

Page 9: Processing String Data and Binary Data (continue)

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 else

exit from REPNE cycle

Page 10: Processing String Data and Binary Data (continue)

Replicating a pattern

PATTERN DB ‘|----|’

RESULT DB 42 DUP (‘ ‘)

MOV CX, 21

LEA DI, RESULT

LEA SI, PATTERN

REP MOVSW

Page 11: Processing String Data and Binary Data (continue)

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 12: Processing String Data and Binary Data (continue)

page 60,132TITLEMovePractice 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 21HMAIN ENDP

CODESEG ENDSEND MAIN ;End of program

Page 13: Processing String Data and Binary Data (continue)

Practice

4. Modify your code so that:- Define: BUS_TITLE DB ‘COMPUTER WIZARDS’- WORK_SPACE DB 16 DUP(20H)

a. - Move BUS_TITLE to WORK_SPACE from left to right (CLD)

b. - MOV BUS_TITLE to WORK_SPACE from right to left (STD)

c. - COMPARE BUS_TITLE with WORK_SPACE, print “equal” if equal, print “not equal” if not5. Compile and run

Page 14: Processing String Data and Binary Data (continue)

Code

; Data segment declaration

EQUALPROMPT DB 'Equal', '$'

NOTEQUALPROMPT DB 'Not Equal', '$'

BUS_TITLE DB 'COMPUTER WIZARDS','$'

WORK_SPACE DB 17 DUP(20H)

Page 15: Processing String Data and Binary Data (continue)

Code ; 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 MOV CX, 17 REP MOVSB

Page 16: Processing String Data and Binary Data (continue)

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 exitEQUALPRINT: MOV AH, 09H LEA DX, EQUALPROMPT INT 21H

Page 17: Processing String Data and Binary Data (continue)

Binary Data

ADC CBW NEG SBB

Page 18: Processing String Data and Binary Data (continue)

ADC

Add with Carry.

Algorithm:

operand1 = operand1 + operand2 + CF

Page 19: Processing String Data and Binary Data (continue)

Example

STC ; set CF =1

MOV AL, 5 ; AL = 5

ADC AL, 1 ;

AL =?

Page 20: Processing String Data and Binary Data (continue)

CBW

Convert byte into word.

Algorithm:

if high bit of AL = 1 then: AH = 255 (0FFh)

else AH = 0

Page 21: Processing String Data and Binary Data (continue)

Example

MOV AX, 0 ; AH = 0, AL = 0

MOV AL, -5 ; AX = 000FBh (251)

CBW ; AX = 0FFFBh (-5)

Page 22: Processing String Data and Binary Data (continue)

NEG

Negate. Makes operand negative (two's complement).

Algorithm:

Invert all bits of the operand

Add 1 to inverted operand

Page 23: Processing String Data and Binary Data (continue)

Example

MOV AL, 5 ; AL = 05h

NEG AL ; AL = 0FBh (-5)

NEG AL ; AL = 05h (5)

Page 24: Processing String Data and Binary Data (continue)

SBB

Subtract with Borrow.

Algorithm:

operand1 = operand1 - operand2 - CF

Page 25: Processing String Data and Binary Data (continue)

Example

STC ; Set CF=1

MOV AL, 5

SBB AL, 3

AL =?

Page 26: Processing String Data and Binary Data (continue)

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

Page 27: Processing String Data and Binary Data (continue)

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

Page 28: Processing String Data and Binary Data (continue)

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)

Page 29: Processing String Data and Binary Data (continue)

Define doubleword using words

Assume, we have 13290147H, we can define this as:

DDVAR DW 0147H

DW 1329H

Page 30: Processing String Data and Binary Data (continue)

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

Page 31: Processing String Data and Binary Data (continue)

page 60,132TITLE 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 21HMAIN ENDPCODESEG ENDSEND MAIN ;End of program

Page 32: Processing String Data and Binary Data (continue)

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