Beruflich Dokumente
Kultur Dokumente
Lab Manual #4
The logic operations are the software analogy of logic gates. They are commonly used to separate a bit or a group of bits in a register or in a memory location, for the purpose of testing, resetting or complementing. The Shift Operations: The shift operations are used to multiply or divide a number by another number that is a power of 2 (i.e. 2n or 2 (n). Multiplication by 2 is achieved by a one-bit left shift, while division by 2 is achieved by a one-bit right shift. The Shift Arithmetic Right (SAR) instruction, is used to manipulate signed numbers. The regular Right Shift (SHR) of a signed number affects the sign bit, which could cause numbers to change their sign. The SAR preserves the sign bit by filling the vacated bits with the sign of the number. The rotate operations are very similar to the shift operations, but the bits are shifted out from one end of a number and fed back into the other end to fill the vacated bits..They are used to reposition a certain bit of a number into a desired bit- location. The rotate right or left instructions through the carry flag (RCL and RCR) are similar to the regular rotate instructions (ROL and ROR), but the carry flag is considered as a part of the number. Hence, before the rotate operation, the carry flag bit is appended to the number as the least significant bit in the case of RCL, or as the most significant bit in the case of RCR.
1/4
Lab Manual #4
Program1
; This program shows the effect of the logic instructions
.MODEL SMALL .STACK 200 .DATA NUM1 NUM2 .CODE .STARTUP
DW DB
0FA62H 94H
MOV AX, NUM1 AND AX, 0FFDFH OR AL, 20H XOR NUM1, 0FF00H NOT NUM2 XOR AX, AX MOV AX, NUM1 AND AX, 0008H XOR AX, 0080H .EXIT END
; load AX with number NUM1 ; Reset 6th bit of AX ; Set 6th bit of AL ; Complement the high order byte of NUM1 ; Complement NUM2 ; Clear AX ; Isolate bit 4 of NUM1 ; Complement 4th bit of AX
2/4
Lab Manual #4
Statement MOV AX, NUM1 AND AX, 0FFDFH OR AL, 20H XOR NUM1,0FF00H NOT NUM2 XOR AX, AX MOV AX, NUM1 AND AX, 0008H XOR AX, 0080H
Hex Output
Using shift and rotate instructions we can show contents of registers or memory locations in hexadecimal numbers. BX register contains 16 bits, which equal four hex digit values. To output the contents of BX, we start from the left and get hold of each digit, convert it to a hex character, and output it. The algorithm which follows is similar to that of binary output. Algorithm for Hex Output: For 4 times DO Move BH to DL /*BX holds output values*/ Shift DL 4 times to the right IF DL < 10 Then Convert to character in 0 9 ELSE Convert to character in A F END_IF Output character Rotate BX left 4 times END_FOR
Lab Manual #4
TYPE A CHARACTER: A THE ASCII CODE OF A IN BINARY IS 01000001 THE NUMBER OF 1 BITS IS 2 3) Write a program that prompts the user to enter a character and prints the ASCII code of the character in hex on the next line. Repeat this process until the user types a carriage return. Sample Execution: TYPE A CHARACTER: Z THE ASCII CODE OF Z IN HEX IS 5A TYPE A CHARACTER:
4/4