简体   繁体   English

如何将两位数相除

[英]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. 我需要显示问题,用户将回答Y或N。我总共有5个问题,其中1个问题有20分。 I will need something like 5 * 20=100. 我将需要5 * 20 = 100。 When the user answer Y, the countY db 0 will increase by 20 当用户回答Y时, countY db 0将增加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). 我已经成功计算出标记,但是如何显示标记是一个两位数的数字(例如80),也可能是一个3位的数字(例如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 . 进行这种划分时,您的方法是正确的,但很遗憾,上面的几行破坏了countY中的值。


Once you get the quotient and remainder from the division, you need to display them with DOS. 从除法器中获得商和余数后,需要使用DOS显示它们。 First the quotient, then the remainder. 首先是商,然后是余数。 But you must not forget to turn them into characters by adding 30h to each. 但是您一定不要忘记将每个字符加30h来将它们变成字符。

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. 唯一缺少的是分数可能精确为100,因此需要3位数字的情况。
Just detect it, display a leading "1", subtract 10 from the quotient, and continue as before: 只需检测它,显示前导“ 1”,从商中减去10,然后像以前一样继续:

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. 通过将cbw更改为mov ah,0 ,此版本的代码可以显示0到199之间的所有数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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