T.243
T.243

Reputation: 35

Conversion of Fahrenheit to Celsius with MASM assembler

I am learning how to code low-level language and I am trying to write a program that will get the degree Fahrenheit from the user then convert it into degree Celsius with this operation

( 5/9 ) * (input- 32) 

To get the result I first compute

5*(input - 32) 

which leaves the product in ax and then divide by 9, but then I am getting the wrong result.
Thanks for the help.

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

I just edited the code, now I am getting the right answers when the Celsius is positive but when my answer(Celsius) is negative the result is wrong I am not sure how to get a negative degree celsius

Upvotes: 2

Views: 2666

Answers (2)

T.243
T.243

Reputation: 35

I figured out why I could not get any negative result (degree). Before, the size of my register was too small to hold bits for negative numbers so in order to correct that I used EAX registers to make the space way bigger.

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 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

Upvotes: 0

Joshua
Joshua

Reputation: 43317

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

When in doubt of something like this, disassemble your code again. I distinctly remember imul and idiv not liking non-register arguments when using masm

Also, always put ? after non ? dw values. Crazy impossible bugs otherwise.

Upvotes: 2

Related Questions