TASM bit operations
I need to show all numbers from matrix(3x3) where number开发者_运维技巧 has two first bits set. I think i'm somewhere near the solution, but something is wrong, can you point what is wrong?
.model small
.stack 100h
.data
n equ 3
a dw n*n dup(?)
.code
extrn write:near
extrn read:near
begin:
mov ax,@data
mov ds,ax
mov cx,n*n
mov si,0
m1:
call read
mov a[si],ax
add si,2
loop m1
mov si,0
mov cx,n*n
m2:
mov bx, a[si]
test bx,1
jz net
test bx,2
jz net
mov ax,a[si]
call write
net:
add si,2
loop m2
mov ah,4ch
int 21h
end begin
It depends on what you mean by "two first bits set." The code you've written works fine if you're looking for the two lowest-order bits. That is, bit 0 and bit 1 are set. The example you give in the comments (3, 6, 7), probably outputs 3 and 7 because they have their lowest-order two bits set. That is:
3 = 00000011 binary
6 = 00000110 binary
7 = 00000111 binary
So only 3 and 7 meet the conditions you've written in your code.
You appear to be interpreting "two first bits" to mean that the number starts with the binary sequence "11" after any leading zeros. If that's really the interpretation you want, then you'll have to use the left shift instruction until the high bit is set to 1, and then check if the next-to-highest bit is set to 1.
精彩评论