1

I was supposed to write code in Assembly (.COM) that computes the given data according to the formula (b * b - 4 * a) / d. The program works fine, but I got 2 questions that I need to answer, but unfortunately I don't know the exact solution.

  1. Why would this program without xor dx, dx not work?
  2. Why the result of the division is not necessarily in the AL register?
               .MODEL  TINY   

Code            SEGMENT           
              
                ORG     100h  
                ASSUME  CS:Code, DS:Code, SS:Code 

Start:                            
              jmp Begin   
              
              a               EQU      20         
              b               EQU      10         
              d               EQU      3          
              Result          DB       ?          

Begin:                                            
              mov     al, b               
              mov     bl, b               
              mul     bl                  
              mov     bx, ax              
                                          
           
              mov     al, a               
              mov     ah, 4               
              mul     ah                          
                                          
              
              sub     bx, ax              
              mov     ax, bx                          
              
              xor     dx, dx                                  
              mov     bx, d                           
              div     bx                          
                                          
              mov     Result, al                              

              mov     ax, 4C00h           
              int     21h                 
                                          

Code           ENDS                           
                END    Start

Please help me with answering these 2 questions. Thank you!

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Kilifu
  • 23
  • 5
  • 1
    Please refer to the instruction set reference as to how `div` works. Note that it divides `dx:ax` by its operand, so if you don't clear `dx` beforehand, you'll divide whatever content is in there, too (and likely cause a division overflow). As for your second question, the result is in `ax`, not `al`. – fuz Apr 07 '21 at 19:30
  • Thank you for answer! Could I still ask why "mov Result, al" works if the result is in ax? – Kilifu Apr 07 '21 at 19:33
  • 1
    Recall that `AL` refers to the low 8 bit of `AX`. – fuz Apr 07 '21 at 19:34
  • Well. Now I know, thank you again! – Kilifu Apr 07 '21 at 20:35
  • If you `xor` a register with itself, you've set the register's value to zero. This is usually preferred over `mov dx,0` since `xor dx,dx` doesn't need to embed the 0 into the machine code to get the job done, making your program's file size smaller. However, if you need to clear a register without altering the flag register, you have to use `mov dx,0`. – puppydrum64 Dec 22 '22 at 18:06

0 Answers0