1

Trying in assembly 8086 to divide two numbers and get the result of division and the remainder
for some reason it doesn't do what I want

 .MODEL SMALL
 .STACK 64
 .DATA
          ResultOfDivision db ?
          Divisor db ?
          Dividend db ?
          Remainder db ?
     .CODE

start: mov ax, @DATA mov ds, ax

      mov    ah, 01
      int    21H
      sub    al, 48
      mov    Divisor, al

      mov    ah, 01
      int    21H
      sub    al, 48
      mov    Dividend, al
      mov    bl, 00
      mov    al, 00
      mov    bl, Divisor
      mov    al, Dividend
      div    bl



      mov    ResultOfDivision, al
      mov    Remainder, ah

      mov    dl, ResultOfDivision
      add    dl, 48
      mov    ah, 02
      int    21H

      mov    dl, Remainder
      add    dl, 48
      mov    ah, 02
      int    21H
      mov    ah, 4ch
      int    21H

       .EXIT

END start

Tion Edi
  • 3
  • 1
  • 3
  • That reason is called CPU design. It will not work as you want, it will instead work, as it was created, and so it does. And stack overflow is full of `x86 div assembly` questions, I wonder how one ends up creating new question on SO instead of checking the Instruction reference guide first. Plus you managed to describe it with "doesn't do what I want", which is actually quite close to "it does not work", both are completely useless for people trying to help you. If you would say the result is `r` for 8/4 input, it would be at least something. – Ped7g May 30 '17 at 10:38
  • Right, I'll try to explain better. When I put a number, and tries to divide it into automatically entered gibberish. for example: 4/num. when i put the " / " sign it's put automatically 4/>>3 – Tion Edi May 30 '17 at 10:53
  • 1
    @TionEdi read the instruction reference guide, description of `DIV`. (also trying to explain it better) – Ped7g May 30 '17 at 10:54
  • it's o.k? or else? – Tion Edi May 30 '17 at 10:58
  • Sure it is ok, you asked the CPU to produce gibberish, and it does, I'm not sure where you see the problem. If you enter as input 8 and 4, do you receive on output `'r'` and `'0'`? If yes, then it works correctly, just like I did run it in my head (that's why I'm asking for confirmation, I have no instant means to actually run that code on real HW). And if you would read carefully, what you wrote in the code, and what is described in the Intel's docs, you would get to the same result. You can also check in debugger, what are the actual register values ahead of each instruction. – Ped7g May 30 '17 at 11:02
  • About `'/'` in the input.. your code doesn't handle that, I mean if I read it correctly, to calculate 8/4 you have to enter 8 and 4 (only two keystrokes) – Ped7g May 30 '17 at 11:04

0 Answers0