首页 > 解决方案 > 将 AH 存储到 DW 变量中时“操作数类型不匹配”

问题描述

嘿伙计们,我的汇编代码有点问题

第 55,60 行操作数类型不匹配我是否试图将 16 位内存移动到 8 位或其他方式?如果是这样,我该如何解决我的问题?谢谢你!

; NUM%
;
    .MODEL SMALL
    .STACK 100h
    .DATA
Ten         DW 10
Two         DW 2
Num         DW 0  ; variable to save Num input from user 
Dig         DW 0  ; Variable to save Dig input from user
Half_Dig    DW 0  ; Variable to save Dig/2
Result      DW 0  ; Variable to store result  from Num^2%dig
PromptStr1  DB 13,10,'Please Enter Number (from 00 up to 99)',13,10,'$';string output to user
PromptStr2  DB 13,10,'Please Enter Digit (from 0 up to 9)',13,10,'$' ;string output to user 
ResultStr   DB 13,10,'XX^2 mod X = X',13,10,'$';Result output to user
ResultStr1  DB 13,10,'The result shloud be round up',13,10,'$'
ResultStr2  DB 13,10,'The result shloud be round down',13,10,'$'
DivisionErr DB 13,10,'Division Error',13,10,'$'

            .CODE             
           MOV AX,@DATA
           MOV DS,AX
           MOV AH,9
           MOV DX,OFFSET PromptStr1
           INT 21h
           ;  Reading First Part of Num
           MOV AH,1
           INT 21h
           MOV ResultStr[2],AL
           SUB AL,'0'
           MOV AH,0
           MUL Ten
           MOV Num,AX
           MOV AX,0
           ;  Reading Second Part of Num
           MOV AH,1
           INT 21h
           MOV ResultStr[3],AL
           SUB AL,'0'
           MOV AH,0
           ADD Num,AX
           MOV AH,0
           ; Reading Dig
           MOV AH,1
           INT 21h
           MOV ResultStr[11],AL
           SUB AL,'0'
           MOV AH,0
           MOV Dig,AX
           MOV AX,0
           ; checking to see if dig==0 , if so divsion error
           CMP Dig,0
           JE Error
           MOV AX,Dig
           DIV Two
           ***MOV Half_Dig,AH***
           MOV AX,0
           MOV AX,Num
           MUL Num
           DIV Half_dig
           ***MOV Result, AH***

希望能帮助我在这里做错了什么!

标签: assemblyx86-16

解决方案


查明问题

  MOV AX,@DATA
  MOV DS,AX
  MOV AH,9
  MOV DX,OFFSET PromptStr1
  INT 21h
  ;  Reading First Part of Num
  MOV AH,1
  INT 21h
  MOV ResultStr[2],AL
  SUB AL,'0'
  MOV AH,0
? MUL Ten                           Does not need the WORD sized multiplication
  MOV Num,AX
? MOV AX,0                          Redundant
  ;  Reading Second Part of Num
  MOV AH,1
  INT 21h
  MOV ResultStr[3],AL
  SUB AL,'0'
  MOV AH,0
  ADD Num,AX
? MOV AH,0                          Redundant
  ; Reading Dig
  MOV AH,1
  INT 21h
  MOV ResultStr[11],AL
  SUB AL,'0'
  MOV AH,0
  MOV Dig,AX
? MOV AX,0                         Redundant
  ; checking to see if dig==0 , if so divsion error
  CMP Dig,0
  JE Error
  MOV AX,Dig
? DIV Two                          WORD sized division requiring DX=0
? ***MOV Half_Dig,AH***            Quotient is in AL instead
? MOV AX,0                         Redundant
  MOV AX,Num
  MUL Num
  DIV Half_dig                     Danger! Half_dig could be zero
? ***MOV Result, AH***             Quotient is in AX instead

您的代码最需要的是将变量定义为字节

Ten         DB 10
Num         DB 0  ; 0-99
Dig         DB 0  ; 0-9
Half_Dig    DB 0  ; 1-4
Result      DB 0  ; 0-3

然后你需要清楚你想要计算什么。你的消息说:XX^2 mod X但你的程序试图计算XX^2 mod (X/2)。它是哪一个?

在下面的代码中,我将使用Half_Dig方法......

应用这些更改

  MOV AX,@DATA
  MOV DS,AX
  MOV AH,9
  MOV DX,OFFSET PromptStr1
  INT 21h
  ;  Reading First Part of Num
  MOV AH,1
  INT 21h
  MOV ResultStr[2],AL
  SUB AL,'0'
  MUL Ten
  MOV Num,AL
  ;  Reading Second Part of Num
  MOV AH,1
  INT 21h
  MOV ResultStr[3],AL
  SUB AL,'0'
  ADD Num,AL
  ; Reading Dig
  MOV AH,1
  INT 21h
  MOV ResultStr[12],AL
  SUB AL,'0'
  MOV Dig,AL

  ; checking to see if (dig/2)==0 , if so divsion error
  SHR AL, 1
  JZ Error
  MOV Half_Dig, AL
  MOV AL, Num
  MUL AL
  DIV Half_dig
  MOV Result, AL

您不需要除以除以 2。简单SHR的工作做得更好。

这使用:ResultStr DB 13,10,'XX^2 mod (X/2) = X',13,10,'$'


推荐阅读