Reputation: 37
These are the problems:
I have this hw, but I only know basic knowledge. It will be a great help if anyone can give me a hint on how to finish these problems. I know this may be a desperate move, but atleast I will learn how to answer these kind of problems.
Upvotes: 1
Views: 794
Reputation: 39166
To clear the most significand nibble and set the least significand nibble of AX you can write:
and ax, 0FFFh ;Clears the top nibble
or ax, 000Fh ;Sets the bottom nibble
Without and
/or
, using shifts:
shl ax, 4 ;Moves the middle part into AH
mov al, 0F0h ;Prepares for a set low nibble
shr ax, 4 ;Puts everything in place
To calculate 10*AX using shifts:
shl ax, 1 ;Calculate 2*AX
mov dx, ax ;Store in DX
shl ax, 2 ;Continue calculating 8*AX
add ax, dx ;Makes AX = 8*AX + 2*AX
Upvotes: 0
Reputation: 2731
"and" returns 1 only, if both input bits are 1.
0 and 0 is 0
1 and 0 is 0
0 and 1 is 0
1 and 1 is 1
so "clearing" a bit can be done by "anding" with 0. whatever the bit (b) is, b and 0 is 0
"or" returns 1, if either of the input bits is 1.
0 or 0 is 0
1 or 0 is 1
0 or 1 is 1
1 or 1 is 1
"setting" a bit can be done by "ORing" with 1. whatever the bit (b) is, b or 1 is 1
AX is 16 bit
if you AND AX with 0000.1111.1111.1111b (= 0x0FFF ) the topmost nibble (4 bits) will be cleared
(AND with 1 will not change a bit, it remains 1 if it's set, and remains unset if it's 0, so all nibbles that should not be changed are ANDed with 1)
if you OR AX with 0000.0000.0000.1111b ( = 0x000F ) the lower nibble will all be set.
(OR with 0 will not change a bit, it remains 1 if it's set, and remains unset if it's 0, so all other nibbles are ORed with 0)
Upvotes: 1
Reputation: 111
The best way to understand these operations is to walk through them step by step. I'll try to explain the process as best I can. With each step I'll include the status of both registers so you can follow along. In order to maintain the upper values of EAX, we have to first move its contents into another register for manipulation.
Start:
EAX: 0011 1100 1001 1111 1011 1100 1001 1111
mov ebx, eax ; copies contents of eax into ebx
EAX: 0011 1100 1001 1111 1011 1100 1001 1111
EBX: 0011 1100 1001 1111 1011 1100 1001 1111
shl ebx, 24 ; this clears the bits off ebx that we're not concerned with
shr ebx, 24 ; shifts back to initial position
EAX: 0011 1100 1001 1111 1011 1100 1001 1111
EBX: 0000 0000 0000 0000 0000 0000 1100 1001
shl ebx, 4 ; shift to the left by 4 bits. bx is where we want it for the next step.
EAX: 0011 1100 1001 1111 1011 1100 1001 1111
EBX: 0000 0000 0000 0000 0000 1100 1001 0000
add bl, 0Fh ; adds 0xF to bl. Or, in binary, 1111. least significant nibble set.
EAX: 0011 1100 1001 1111 1011 1100 1001 1111
EBX: 0000 0000 0000 0000 0000 1100 1001 1111
xor ax, ax ; clears contents of ax, making room for what's in bx
EAX: 0011 1100 1001 1111 0000 0000 0000 0000
EBX: 0000 0000 0000 0000 0000 1100 1001 1111
add ax, bx ; combine the results of bx back into eax for the end result
EAX : 0011 1100 1001 1111 0000 1100 1001 1111
And there you have it! Now that you're familiar with shift operations, I'll leave the last part to you.
Cheers!
Upvotes: 0
Reputation: 100602
You can set and clear parts of an integral value using bit logic — logical AND and OR in that case.
Superficial examples: I have the bit value 0101; I want to set the lowest two bits. So I OR with 0011. Now I want to clear the two lowest bits. So I AND with 1100.
Upvotes: 0