简体   繁体   中英

How to divide two digit number

I needed to display out Questions and user will answer Y or N. I have a total of 5 questions, and 1 questions have 20 marks. I will need something like 5 * 20=100. When the user answer Y, the countY db 0 will increase by 20

I had successfully calculate the marks, but how to display out as the marks is a two digit number (Eg. 80) and it might also is a 3 digit number (Eg. 100).

Q1: 
    mov ah, 09h
    lea dx, msgq1
    int 21h
    mov ah, 01h
    int 21h
    mov myInput, al
    cmp myInput, 59h
    JE I1
    jmp Q2


  I1:
    mov dl, countY
    add dl,20
    mov countY, dl

  ;calculation
  Cal:
    mov ah,02h
    mov dl, countY
    add dl, 30h  ; display countY=80;
    mov countY, dl
    int 21h

    ;NOT WORKING, ERROR CODE
    mov bl,10
    mov al, countY
    cbw
    div bl

    mov q, al
    mov r, ah

    mov ah, 02h
    mov q, al
    int 21h
 cal: mov ah,02h mov dl, countY add dl, 30h ; display countY=80; mov countY, dl int 21h ;NOT WORKING, ERROR CODE mov bl,10 mov al, countY cbw div bl 

With this division you were on the right way, but it's too bad that the few lines above it did destroy the value in countY .


Once you get the quotient and remainder from the division, you need to display them with DOS. First the quotient, then the remainder. But you must not forget to turn them into characters by adding 30h to each.

cal:
  mov bl,10
  mov al, countY     ;Values are {0,20,40,60,80}
  cbw                ;Prepare for division of AX/BL
  div bl             ; -> AL=quotient AH=remainder
  mov  dx, ax        ;Conveniently moving both to DX
  add  dx, 3030h     ;Adding 30h to each in a single instruction
  mov  ah, 02h
  int  21h           ;Display the tenths
  mov  dl, dh
  mov  ah, 02h
  int  21h           ;Display the ones

The only thing that's missing is the case where the score could be 100 precisely, thus needing 3 digits.
Just detect it, display a leading "1", subtract 10 from the quotient, and continue as before:

cal:
  mov  bl,10
  mov  al, countY    ;Values are {0,20,40,60,80,100}
  mov  ah, 0         ;Prepare for division of AX/BL
  div  bl            ; -> AL=quotient AH=remainder
  cmp  al, 10
  jl   Skip
  push ax            ;Save AX because the DOS call destroys it's value
  mov  dl, "1"
  mov  ah, 02h
  int  21h           ;Display the hundreds
  pop  ax            ;Restore AX
  sub  al, 10
Skip:
  mov  dx, ax        ;Conveniently moving both to DX
  add  dx, 3030h     ;Adding 30h to each in a single instruction
  mov  ah, 02h
  int  21h           ;Display the tenths
  mov  dl, dh
  mov  ah, 02h
  int  21h           ;Display the ones

By changing cbw into mov ah,0 , this version of the code can display all numbers ranging from 0 to 199.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM