2

(Processor Intel x86)

.MODEL SMALL

Print   EQU 2
Exit    EQU 4Ch

.DATA ;------------------------------------------------------

a   DW  8
b   DW  2

.CODE ;------------------------------------------------------

Start   PROC

    mov ax, SEG DGROUP
    mov ds, ax

    mov ax, a
    ;div    b <---- uncommenting it makes program hang

    mov dx, ax
    add dx, '0'

    mov ah, Print      
    int 21h
    mov ah, Exit
    int 21h

; -----------------------------------------------------------

Start   ENDP
    .STACK 512

    END Start

If compiled with Turbo Assembler, the code outputs 8. If i uncomment the DIV command, the code hangs.

Looking at the registers with Turbo Debugger, I saw that the DIV command sends the result of division into the AX register -- the same register which stored the number 8 and which will be sent to DX to be printed.

What did cause in DIV to hang my program? Did I oversee DIV putting data into registers which were to be used by some important background functions or... something?

Jester
  • 56,577
  • 4
  • 81
  • 125
dziadek1990
  • 277
  • 2
  • 10

1 Answers1

6
mov ax, a
;div    b

Since b is a word sized variable the div b instruction divides DX:AX by the word b. You forgot to setup DX with a zero! The division overflows and an exception was triggered.

mov ax, a
xor dx, dx
div b
Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • What does the colon in the DX:AX register mean? And how is DX:AX register to be treated in calculations? From what you're saying, I understand DX:AX is equal to AX if DX is equal zero, right? – dziadek1990 Apr 17 '15 at 16:48
  • 2
    The colon is just an agreed syntax to express that DX contains the top 16 bits of an 32 bit quantity. (AX holds the bottom 16 bits) – Fifoernik Apr 17 '15 at 16:50