0

I can't wrap my head around this problem. I want to print all numbers from 0 - 255 divisible by user input ( 1 - 9), yet the way I'm trying to do it I would need more registers, because currently the registers would be overwritten.

Is there any easier / simpler way of doing this?

org 100h
section .text
mov ah,1
int 21h
sub al,'0'


mov bl,al

mov cx,255
@LOOP:
mov ax,cx
mov ah,0
xor dx, dx
div bx
cmp dx, 0
je ;redirect to printing loop  
loop @LOOP

;PRINTING LOOP
mov     bx,10          
xor     cx,cx          
@a: xor     dx,dx        
div     bx             
push    dx             
inc     cx             
test    ax,ax         
jnz     @a             
@b: pop     dx          
add     dl,"0"         
mov     ah,02h         
int     21h            
loop    @b
;PRINTING LOOP

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    You don't seem to be using `si`, `di` or `bp` at all. x86 does not have a lot of registers but you might as well use the ones that exist :) When you truly run out of them, you can use local variables on the stack. – Jester Apr 10 '20 at 17:48
  • 1
    You only need division for breaking an integer into digits, not for divisibility tests. To get all multiples of `x` just do `i += x` in a loop until it wraps (carry flag). If you want to start from the highest multiple and count down then you would want division to find that highest multiple <= 255 though. – Peter Cordes Apr 10 '20 at 18:39
  • push/pop to print the digits in printing order is fairly simple but not every efficient. If you're going to use temporary space on the stack to store digits anyway, might as well reserve a buffer and store into it in the `div` loop, to build up an ASCII string you can pass to a single system call. Like in [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) – Peter Cordes Apr 10 '20 at 19:12

0 Answers0