0
.model small
.stack 512
.data
   cad  db 9 dup (' '),'$'
   var1 db ?
   num  db ?
   aux  db ?

  .code
   .startup
   mov var1,0   
   mov ah,01h  
   int 21h     
   sub al,30h   
   mov num,al   
   mov al,num   
   mov bl,10    
   mul bl       
   mov aux,al   
   mov var1,0   
   mov ah,01h
   int 21h      
   sub al,30h
   add aux,al   
   mov bl,aux   
   mov num,bl   
   mov ah,02h   
   mov dl,'='
   int 21h
   mov SI,6        
   bin:             
      mov Ah,00h    
      mov Al,num
      mov Bl,2
      div Bl
      mov var1,Ah
      mov num,Al
      mov dl,var1
      add dl,30h
      mov cad[SI],dl    
      cmp num,1         
      dec SI
      jne bin      
      je out    
      cmp num,0     
      jne bin
      je out
   out:          
   mov dl,num    
      add dl,30h
      mov cad[SI],dl
  mov ah,09h
  lea Dx,cad
  int 21h

.exit
end

So I got this asm program which when I input two digit numbers it turns them into binary and I want to modify it for three numbers. But I don't know yet much about assembler so I need some help understanding what I should change and what I shouldn't to be able to achieve this. I've tried changing some values but not luck so far.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Diego Esquivel
  • 182
  • 1
  • 12
  • It uses `div` to divide by 2. That horribly inefficient and not a good starting point; use shifts to extract bits, and either FLAGS (CF) or `and reg, 1` to get 1 bit at a time. – Peter Cordes May 06 '20 at 20:34
  • But doesnt it divie by 2 cause like thats what you need to do to get a base 10 number into base2 – Diego Esquivel May 06 '20 at 20:36
  • So I add from mov var1, 0 to mov aux, al once again so it does it three times, now it takes three numbers but it prints it in a wwrong way – Diego Esquivel May 06 '20 at 20:41
  • You just need to extract one bit at a time; it doesn't have to be by division at all. You can extract from the top of the register. e.g. `xor dx,dx` / `shl ax, 1` / `adc dx, '0'` or similar.. Or if you do want to divide by 2, you don't have to use a `div` instruction. `shr ax,1` will do it *much* more efficiently. – Peter Cordes May 06 '20 at 20:44
  • Duplicate of https://stackoverflow.com/questions/52547515/decimal-to-binary-conversion-asm-code-optimization – Erik Eidt May 06 '20 at 20:58
  • Original version possibly in spanish: https://steemit.com/cervantes/@fintechresearch/challengue-que-hace-este-programa – Erik Eidt May 06 '20 at 21:07
  • I see, shame none has the answer of how I can solve this – Diego Esquivel May 06 '20 at 21:20
  • Nevermind I managed to do it – Diego Esquivel May 06 '20 at 21:39

1 Answers1

0

Nevermind I manage to do it, here is how if anyone is interested

.model small
.stack ;512
.data
   cad  db 9 dup (' '),'$'
   var1 db ?
   num  db ?
   aux  db ?

  .code
   .startup
   mov var1,0   
   mov ah,01h   
   int 21h      
   sub al,30h   
   mov num,al   
   mov al,num   
   mov bl,10    
   mul bl     
   mov aux,al   

   mov var1,0   
   mov ah,01h
   int 21h      
   sub al,30h
   add aux,al   
   mov al,aux  
   mov num,al   
   mov bl,10    
   mul bl      
   mov aux,al   

   mov var1,0   
   mov ah,01h
   int 21h      
   sub al,30h
   add aux,al   
   mov bl,aux  
   mov num,bl   
   mov ah,02h   
   mov dl,'='
   int 21h
   mov SI,8        
   bin:             
      mov Ah,00h   
      mov Al,num
      mov Bl,2
      div Bl
      mov var1,Ah
      mov num,Al
      mov dl,var1
      add dl,30h
      mov cad[SI],dl    
      cmp num,1       
       dec SI
       jne bin      
       je out   
       cmp num,0     
       jne bin
       je out
   out:          
   mov dl,num    
      add dl,30h
      mov cad[SI],dl
  mov ah,09h
  lea Dx,cad
  int 21h

.exit
end
Diego Esquivel
  • 182
  • 1
  • 12