0

this is the source code which i am using SOURCE: https://ankurm.com/8086-assembly-program-to-count-number-of-0s-and-1s-from-a-number/

DATA SEGMENT
NO DW 5648H
Z DW ?
O DW ?
DATA ENDS
 
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AX, NO
MOV BX, 00H
MOV CX, 10H
MOV DX, 00H
 
UP:
ROL AX,1
JC ONE
INC BX
JMP NXT
 
ONE:
INC DX
 
NXT:
DEC CX
JNZ UP
 
MOV Z, BX
MOV O, DX
 
INT 3
CODE ENDS
END START
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 3
    Note that on a CPU which isn't antique, you can replace the whole thing with an invocation of [`POPCNT`](https://www.felixcloutier.com/x86/popcnt). – Nate Eldredge May 05 '22 at 14:47
  • 2
    You already have working code, it looks like. A bit inefficient (e.g. could use `adc dx, 0` instead of branching, and IDK why it has two separate counters instead of getting the zero count as 16-ones after the loop). You can put anything you like in a macro, with comments about where it wants input and which registers it steps on. So what's the problem here? Just macro syntax? Which assembler are you actually using, TASM or EMU8086? – Peter Cordes May 05 '22 at 14:56
  • I don't get the question. The code could be optimized by unrollling e.g. by `top: shr ax,1; adc bx,0; shr ax,1; adc bx,0; shr ax,1; jz out; adc bx,0; jmp top; out:` -- no need for a counter, as `ax` will be zero eventually. `add ax,ax` and `shr ax,1` are probably equally good -- except that shr favours small numbers. – Aki Suihkonen May 05 '22 at 15:15
  • Thank you for answering, i am using tasm. i am very beginner in assembly programming can you please specify where exactly should i put the macro and call it again – Chirag Adve May 06 '22 at 06:45
  • Here's a question with a Tasm Macro that should easily convert to your needs: https://stackoverflow.com/questions/38575023/weird-macros-tasm – Aki Suihkonen May 07 '22 at 06:31

0 Answers0