Lab 4

advertisement

Lab 4

LOGIC INSTRUCTIONS, SHIFT INSTRUCTIONS AND ROTATE

INSTRUCTIONS

Logic Instructions

• AND destination, source

Can be used to clear(0) specific destination.

Ex: b AND 1 = b b AND 0 = 0

• OR destination, source

Can be used to set(1) specific destination.

Ex: b OR 0 = b b OR 1 = 1

• XOR destination, source

Can be used to complement(opposite) specific destination.

Ex: b XOR 0 = b b XOR 1 = ~b

Cont. Logic Instructions

NOT destination

Performs one’s complement (opposite) on destination.

TEST destination, source

Performs AND operation but does not change the destination contents.

Example 1

Enter an ascii digit, convert it to a number then display the ascii of it in a new line.

org 100h

MOV AH, 1

INT 21H ;a function to input a ascii number from user

AND AL,0FH ;converting to a number

MOV BL,AL

MOV AH,9

LEA DX,NEWL

INT 21H ;printing a newline

MOV AH,2

MOV DL,BL

INT 21H ;printing the converted number in ascii code ret

NEWL DB 0DH,0AH,"$"

Example 2

MOV number 5 in BL register and display it as an output.

org 100h

MOV BL,5 ;assign 5 to BL

OR BL,30H ;convert it to an ascii character

;so it can be printed as 5

MOV AH,2

MOV DL,BL

INT 21H ;print the number ret

Clearing

• Clear AX

MOV AX, 0 or

SUB AX, AX or

XOR AX,AX

• Clear MEM

MOV MEM,0

Shift Instructions

• SHL destination, numberOfShifts

₋ Shifts the bits in destination to the left.

₋ numberOfShifts must be a direct value or a value in CL register.

₋ Inserts 0 to the rightmost bit.

₋ Loses the leftmost bit and adds to CF.

₋ Every left shift is a multiplication by 2

• SHR destination, numberOfShifts

₋ Shifts the bits in destination to the right.

₋ numberOfShifts must be a direct value or a value in CL register.

₋ Inserts 0 to the leftmost bit.

₋ Loses the rightmost bit and adds to CF.

₋ Every right shift is a division by 2

Example 3

Multiply 8 by 8 and divide 65 by 4.

org 100h

MOV BL,8

MOV CL,3

SHL BL,CL

MOV BH,65

MOV CL,2

SHR BH,CL ret

Rotate Instructions

• ROL destination, numberOfRotations

₋ Shifts the bits in destination to the left.

₋ numberOfShifts must be a direct value or a value in CL register.

₋ The leftmost bit is inserted in the rightmost bit.

₋ Adds to CF.

• ROR destination, numberOfRotations

₋ Shifts the bits in destination to the right.

₋ numberOfShifts must be a direct value or a value in CL register.

₋ The rightmost bit is inserted in the leftmost bit.

₋ Adds to CF.

Example 4

Assign 55 to BX register then count the number of 1 bits and display it on screen.

org 100h

MOV BX,55

XOR DX, DX

MOV CX, 16

TOP:

ROL BX, 1

JNC NEXT

INC DX

NEXT:

LOOP TOP

OR DX,30H

MOV AH,2

INT 21H ret

; AX counts bits

; loop counter

; CF = bit rotated out

; 0 bit

; 1bit, increment total

; loop until done

Example 5

Input a binary number then display it in a newline.

org 100h

XOR BX,BX ; CLEAR BX

MOV AH,1

INT 21H ; Input character

WHILE_:

CMP AL, 0DH ; CR?

JE END_WHILE ; YES, DONE

AND AL, 0FH ; CONVERT TO BINARY VALUE

SHL BX,1 ; MAKE ROOM FOR NEW VALUE

OR BL,AL ; PUT VALUE IN BX

INT 21H ; READ A CHARACTER

JMP WHILE_ ; LOOP BACK

END_WHILE:

Cont. Example 5

mov ah,9 lea dx,newl int 21h

MOV CX,16

MOV AH,2

TOP_:

ROL BX,1 ;content in BX

JC display_one ; print bit ‘1’

MOV DL,"0" ; else Print bit ‘0’

JMP display display_one:

MOV DL,"1"

Display:

INT 21H ; Print bit either ‘0’ or ‘1’

LOOP TOP_ ; LOOP BACK ret newl db 0ah,0dh,"$"

Exercise 1

Let the user enter 2 numbers 4 and 5 then display the sum of them.

org 100h

;prepare to read first number

MOV AH,1

INT 21H

;save the number in other register

MOV BL,AL

;convert the number to its decimal value

AND BL,0FH

;display addition symbol

MOV AH,2

MOV DL,2BH

INT 21H

;prepare to read second number

MOV AH,1

INT 21H

;convert the number to its decimal value

AND AL,0FH

Cont. Solution

;add the two numbers

ADD BL,AL

;convert the number so it would be printed as its symbol

OR BL,30H

;display a newline that is saved in a variable

MOV AH,9

LEA DX,NWL

INT 21H

;display the result

MOV AH,2

MOV DL,BL

INT 21H ret

NWL DB 0DH,0AH,"$"

Download