使用MASM汇编程序将华氏温度转换为摄氏温度

时间:2018-10-21 22:22:02

标签: assembly masm low-level

我正在学习如何编写底层语言,并且正在尝试编写一个程序,该程序将从用户那里获得华氏度,然后通过此操作将其转换为摄氏度

( 5/9 ) * (input- 32) 

要获得结果,我首先进行计算

5*(input - 32) 

将乘积留在ax中,然后除以9,但结果却是错误的。
感谢您的帮助。

include PCMAC.INC
.model small
.586
.stack 100h
.data
  msg        db 'Enter  the degree in fahrenheit:  $'
  input      dw ?
  cel        dw ?
  outputmsg  db " Conversion done, result is $"
  outputmsg2 db " celsius", 13, 10, '$'
  A          dw 5   
  B          dw 9   
.code
  extrn GetDec:near, PutDec:near
  main      PROC
    _Begin 
    _Putstr msg          
    call GetDec     

    mov input, ax   

    mov bl, 32
    sub al, bl
    ; sub ax, 32

    mov cx, A
    imul cx

    mov cx, B
    idiv cx

    mov cel, ax

    _putstr outputmsg
    call PutDec

    _putstr outputmsg2
    _Exit 0
  main endp
end  main

我刚刚编辑了代码,现在摄氏为正时我得到正确的答案,但是当我的答案(摄氏)为负时结果是错误的,我不确定如何获得负摄氏度

2 个答案:

答案 0 :(得分:2)

Mov cx, [B]
Imul cx
Mov cx, [A]
Idiv cx

如果对此类问题有疑问,请再次反汇编代码。我清楚地记得使用masm时imul和idiv不喜欢非注册参数

也总是放?后非? dw值。否则,疯狂的不可能的错误。

答案 1 :(得分:0)

我弄清楚了为什么我没有得到任何负面结果(度)。以前,我的寄存器大小太小,无法容纳负数位,因此为了更正,我使用了EAX寄存器来增大空间。

包括PCMAC.INC

.model小 .586 。堆栈100小时 .data

msg db'输入华氏度:$'

输入dw吗?

cel dw吗?

outputmsg db“转换完成,结果为$”

outputmsg2 db“ celsius”,13,10,“ $”

dw 5

B dw 9

.code

extrn GetDec:near,PutDec:near   主要PROC

_Begin 

_Putstr msg          


call GetDec     

mov input, ax   

mov ebx, 32
sub eax, ebx
;mov bl, 32
;sub al, bl
; sub ax, 32

mov cx, A
imul cx
;mov cx, A
;imul cx


mov cx, B
idiv cx
;mov cx, B
;idiv cx


mov dx, 0
mov cx, ax
;mov cel, ax

_putstr outputmsg
mov eax, ecx
call PutDec