2

I am trying to print the remainder of division using emu8086.inc library but the remainder shows ascii value when I run the program. What should I write to print the remainder exactly?

.MODEL SMALL
.STACK 100h
.DATA

.CODE
MAIN PROC

INCLUDE 'emu8086.inc'

 DEFINE_SCAN_NUM   ;DEFINE NUMBER FOR SCAN

 DEFINE_PRINT_NUM  ;DFINE NUMBER TO PRINT  



 DEFINE_PRINT_NUM_UNS ;DEFINE UNSIGNED NUMBER TO PRINT

 CALL SCAN_NUM   ;FIRST INPUT
 MOV AX,CX       ;SHIFT THE DATA TO AX

 PUTC 0AH        ;NEW LINE
 PUTC 0DH

                 ;SECOND INPUT
CALL SCAN_NUM
                 ;AX/CX = AX
 IDIV CX
                 ;NEW LINE
 PRINTN ''

 CALL PRINT_NUM  ;PRINT QUOTIENT 
 MOV AH,2
 MOV DL,AL
 INT 21H
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180

1 Answers1

1
;SECOND INPUT
CALL SCAN_NUM
             ;AX/CX = AX
IDIV CX

The comment is wrong! IDIV CX will do a signed divide of DX:AX by CX.
You still need to setup DX.

CWD is an instruction that will sign-extend the AX register into DX:AX. If AX is positive then DX will get 0, and if AX is negative then DX will get -1. This step is necesssary for the IDIV CX instruction to work correctly! It will return the quotient in AX and the remainder in DX.

;SECOND INPUT
CALL SCAN_NUM      ;CX has the second input
CWD                ;AX has the first input, sign extending it to DX:AX
IDIV CX            ;DX:AX / CX

CALL PRINT_NUM  ;PRINT QUOTIENT

This does print the quotient from AX. The remainder from the division is in DX. Just move it to AX and invoke the same print macro procedure for printing signed numbers.

CALL PRINT_NUM  ;PRINT QUOTIENT
mov  ax, dx
CALL PRINT_NUM  ;PRINT REMAINDER

MOV AH,2
MOV DL,AL
INT 21H

This code doesn't do anything useful in your program.

Fifoernik
  • 9,779
  • 1
  • 21
  • 27