Hi. I've created a small program where the user inputs 8 bits and those bits are stored in order of input into an array. Here is a code fragment of my program that deals with this array...

mov CX,0
mov bx, offset array

inc bx;
mov  ah, 01h  ; keyboard input subprogram
int  21h    ; read character into al
mov , al

inc CX
cmp CX, 7
jle arr_input

Now I'd like to see how many times the first two bits are repeated in the byte. For example if the first two characters are 11 then in the byte 11001101, the answer is 2...

Can anybody provide some tips or sample code on how this could be done? Thanks.
Posted on 2009-10-15 22:32:40 by RS
Hmmmm, if the user enters "11100000" is the answer 2, because the second and third match the first two, or do you need "new" characters past the first two?

There are instructions that deal with bits, but mostly only one at a time... and you haven't got "bits" anyway. Probably easier to treat it as a "character match"... IF that will fullfill the assignment. If you intend to limit input to '0' and '1', better enforce it - those pesky users will enter anything that comes into their head, y'know!

As your current code stands, you aren't putting anything at all in the first byte of your array. Is that what you intend?

(always clear up the "specification" for a program before you write it! :)


Posted on 2009-10-16 02:45:00 by fbkotler
Sorry...correct, if a user types in 1110000 then the answer is 2. I intended for the first spot in the array, it gets filled in later in the program. By the time the program is ready to do what I want it do with this counting thing, there will be the full 8 bits.
Posted on 2009-10-16 03:14:36 by RS
Okay... If you really need "bits" in an 8-bit number, there's more processing to do. What you've got is 8 bytes/characters. Should be good enough to count matches. Two bytes will fit in a 16-bit register. Increment your index and compare. If they compare equal increment a (pre-zeroed) counter - probably jump over the "increment counter" if they don't compare equal, actually. Cook until done. Similar to the way you filled the array. You should be able to work it out.


Posted on 2009-10-16 06:06:04 by fbkotler

mov counter, 0 ;set pattern counter to 0
mov cx, 0 ;set loop counter to 0
mov bx, offset array ;to begin working at the beginning of the array
mov dl, ; move first character in array to dl
mov first, dl ;move dl into first...first should now hold the first character in my array
inc bx ;increment index to work with second character
mov dl, ; move second character to dl
mov second, dl ; move dl into second....second should now hold the second characer in my array

inc cx
cmp cx, 8
je print_count ;to eventually exit the loop
inc bx ;to move the 3rd element in array
mov al,
cmp first, al ;compares third element with the array with the first
je check_second ;if they are equal move onto check_second
jmp check_first ;otherwise restart loop

inc cx
cmp cx, 8
je print_count
inc bx
mov al,
cmp second, al ;if program reaches this label, there has been a match with the first character, so we are seeing if the following
je match         ;character matches the second character, if it does, jump to match
jmp check_first ;otherwise restart process at check_first

inc counter       ;there has been two successive characters that follow the same pattern as the first and second character,  
jmp check_first  ;so increment the counter and return to check_first to restart the pattern

print_count: ;prints out the value of counter
mov dl, counter
mov ah, 2h
int 21h

Does this look somewhat right? Unfortunately nothing is printed except the nwln. Do you see any reason why? Thanks again.
Posted on 2009-10-16 13:06:00 by RS
Yes. It looks "pretty much right" (not tested!) up until you print the answer. Try this experiment:

mov dl, 7
mov ah, 2
int 21h

See the '7'? No? Try this modification:

mov dl, 7
add dl, '0'
mov ah, 2
int 21h

That should give you a clue how to fix your program. Fortunately, you'll only have to print one digit (think about how you'd print 123). Consult an ascii chart to see why we need to do this. Notice how the characters for '0' through '9' relate to the numbers 1 through 9...

You got an ascii chart? I never know what you "homework questions guys" have got available for resources... You do need to read the textbook! I notice, for example, that you seem to have "nwln" available. If you've also got "printint" or so, that might be useful...

One other thing you might want to look at. In the event that "first" matches, but "second" doesn't, you may be incrementing bx one too many times and be "skipping" a character you want to check. (might happen if you get a "match", too) Once you get it running and can see the "answer" you can tweak the logic to get the "right" answer...

I suppose you've got an "exit" you didn't show (you should!!!)...


Posted on 2009-10-16 13:56:32 by fbkotler
Appreciate the help, got it working. Thanks a lot man.
Posted on 2009-10-16 15:24:38 by RS
Out of curiosity, how can I modify that code to make it so over lappings count as well?

I.e 1110011 counts as 3.
Posted on 2009-10-19 09:18:15 by RS
Should be a case of where and when you increment bx. You grab the first character and put it in "first", increment bx, grab the second character and put it in "second" then increment bx again so you're starting the comparison with the third character.

You can "back up" with "dec bx". You can also code "mov dl, " to get the second character of a "pair". One or another of those should do it. Keep a close eye on where bx is pointing, and you can figure it out.


Posted on 2009-10-19 09:37:29 by fbkotler