Free2Code
 
Time: 2009-01-07, 01:48am
8086 Assembly Problem
Subject: 8086 Assembly Problem  ·  Posted: 2005-09-11, 03:43pm
Rank: ? (2)
Member #: 25278
Hi

I'm taking an 8086 uProcessor course and I'm stuck on a programming question. First let me say that I'm NOT looking for someone to give me the answer - just some hints about what I'm doing wrong would be great. I'm using emu386 with WinXP, for what that's worth. Also keep in mind that so far we've only studied the basic keywords, nothing fancy. I have a feeling I'm doing this the difficult way, but I'm not sure. After previewing this post, the formatting doesn't look right so I removed most of my comments.

Here's the problem: "Count the number of bits, in the double word that starts at memory location DS:1234h, that are 1. Place the count in register AL."

My code works well up to a count of nine ones. Starting at ten, the count is always minus one. IOW, if there are 9 ones in the word, my code counts 9. If there are ten ones in the word, my code counts nine. If there are eleven ones in the word, my code counts ten, etc.

Here's my code (hope this formats right):
Code:
  1. mov word ptr [1234h],07ffh  ;pick a value for testing
  2. ;0000h = 0000000000000000 (no 1s)    
  3. ;1010h = 0001000000010000 (2 x 1s)
  4. ;000eh = 0000000000001110 (3 x 1s)
  5. ;03feh = 0000001111111110 (9 x 1s)
  6. ;03ffh = 0000001111111111 (10 x 1s)
  7. ;0abcdh = 1010101111001101 (10 x 1s)
  8. ;07ffh = 0000011111111111 (11 x 1s)
  9. mov al, 00h ;AL used to count the number of 1s
  10. mov dx,0000h ;DX used to count to 16          
  11. tmsshift: ;shift DS:1234h
  12. rcl word ptr [1234h],1 ;shift MSB into CF     
  13. inc dx ;inc 16x ctr (using DX)
  14. jc tmsincbitctr ;if CF=1 jump to increment bit counter
  15. jnc tms16xctr ;if CF=0 jump to 16x counter (skip incbitctr)
  16. tmsincbitctr: ;if CF = 1, increment AL
  17. inc AL                  
  18. tms16xctr: ;compare counter to 16. If counter equals 16, end
  19. cmp dx,0fh
  20. jae tmsend
  21. jb tmsshift
  22. tmsend: ;just something to show it's done
  23. mov CX,5555h


Any ideas?

radarfxst

 
  Reply to this ·  Post link ·  Top
Subject: Re: 8086 Assembly Problem  ·  Posted: 2005-09-11, 03:54pm
Rank: ? (2)
Member #: 25278
Sorry, I meant emu8086, not emu386...

- radarfxst

 
  Reply to this ·  Post link ·  Top
Subject: Re: 8086 Assembly Problem  ·  Posted: 2006-10-05, 09:24am
Rank: ? (5)
Member #: 28723
hi,

sure this topic is outdated but i was kind of curious if i could solve this problem ( i'm just beginning to learn assembler myself

this is my solution ( 16bit assembler, masm version ):

.model tiny ;just .com modul
.code
.startup
mov bx,word ptr value ; get the first word of dword
call b10_count1
mov bx,word ptr (value+2) ; get the second part of dword
call b10_count1
mov al,sum
.exit

; examine 1 word at a time
b10_count1:
mov testw,bx
mov bx,1
b10_10:
mov ax,testw
and ax,bx ; test 1 bit
jz b10_20
inc sum ; count the 1's
b10_20:
shl bx,1 ; set next bit to the left
jnz b10_10 ; when bx zero, finished (8000 shl,1 -> 0000 )
ret

; var definitions:
testw dw 0 ;temp var for holding 1 word to be examined
value dd 0ffffffffh ;dword to be tested
sum db 0
end

 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons