The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: peedi on April 30, 2009, 01:25:23 AM

Title: Help converting temperature assignment
Post by: peedi on April 30, 2009, 01:25:23 AM
Hey i need help with converting Celsius to Fahrenheit.
Some reason its giving me a weird output. What seems to be the problem?

here is the original code

     PAGE    80,132
;===================================================================
;
;                  PROGRAM LISTING 3.1
;
; PROGRAM  TO  CONVERT  A  TEMPERATURE  FROM
; FAHRENHEIT TO CENTIGRADE USING THE FORMULA
;
; C = 5*(F-32)/9  ROUNDED TO NEAREST INTEGER
;===================================================================
           DOSSEG
           .MODEL  SMALL,BASIC,FARSTACK
;===================================================================
                                       ;PROCEDURES TO
           EXTRN   GETDEC:FAR          ;GET 16-BIT DECIMAL INTEGER
           EXTRN   NEWLINE:FAR         ;DISPLAY NEWLINE CHARACTER
           EXTRN   PUTDEC:FAR          ;DISPLAY 16-BIT DECIMAL INTEGER
           EXTRN   PUTSTRNG:FAR        ;DISPLAY CHARACTER STRING
;===================================================================
;
; S T A C K   S E G M E N T   D E F I N I T I O N
;
           .STACK  256
;===================================================================
;
; C O N S T A N T   S E G M E N T   D E F I N I T I O N
;
           .CONST
PROMPT     DB      'ENTER TEMPERATURE IN DEGREES FAHRENHEIT   '
ANNOTATION DB      '      TEMPERATURE IN DEGREES CENTIGRADE   '
;===================================================================
;
; C O D E   S E G M E N T   D E F I N I T I O N
;
           .CODE
EX_3_1:
           MOV     AX,SEG DGROUP       ;SET ES-REGISTER TO ADDRESS
           MOV     ES,AX               ;    DGROUP
;
           LEA     DI,PROMPT           ;PROMPT FOR F_TEMP
           MOV     CX,42
           CALL    PUTSTRNG
           CALL    GETDEC              ;GET F_TEMP
           SUB     AX,32               ;C_TEMP = (F_TEMP -32) * 5 / 9
           MOV     BX,5
           IMUL    BX
           MOV     BX,9
           IDIV    BX
           XCHG    AX,DX               ;REMAIN = (F_TEMP-32)*5 mod 9
           MOV     BL,5                ;ROUND  = REMAIN / 5
           IDIV    BL
           CBW
           ADD     AX,DX               ;C_TEMP = C_TEMP + ROUND
;
           LEA     DI,ANNOTATION       ;DISPLAY C_TEMP
;          MOV     CX,42
           CALL    PUTSTRNG
           MOV     BH,0
           CALL    PUTDEC
           CALL    NEWLINE
           MOV     AH,4CH              ;RETURN TO DOS
           INT     21H
           END
;===================================================================



Here is my modified code

   PAGE    80,132
;===================================================================
;
;                  PROGRAM LISTING 3.1
;
; PROGRAM  TO  CONVERT  A  TEMPERATURE  FROM
; FAHRENHEIT TO CENTIGRADE USING THE FORMULA
;
; F = (9*C) /5+32  ROUNDED TO NEAREST INTEGER
;===================================================================
           DOSSEG
           .MODEL  SMALL,BASIC,FARSTACK
;===================================================================
                                       ;PROCEDURES TO
           EXTRN   GETDEC:FAR          ;GET 16-BIT DECIMAL INTEGER
           EXTRN   NEWLINE:FAR         ;DISPLAY NEWLINE CHARACTER
           EXTRN   PUTDEC:FAR          ;DISPLAY 16-BIT DECIMAL INTEGER
           EXTRN   PUTSTRNG:FAR        ;DISPLAY CHARACTER STRING
;===================================================================
;
; S T A C K   S E G M E N T   D E F I N I T I O N
;
           .STACK  256
;===================================================================
;
; C O N S T A N T   S E G M E N T   D E F I N I T I O N
;
           .CONST
PROMPT     DB      'ENTER TEMPERATURE IN DEGREES CELSIUS   '
ANNOTATION DB      '      TEMPERATURE IN DEGREES FAHRENHEIT   '
;===================================================================
;
; C O D E   S E G M E N T   D E F I N I T I O N
;
           .CODE
EX_3_1:
           MOV     AX,SEG DGROUP       ;SET ES-REGISTER TO ADDRESS
           MOV     ES,AX               ;    DGROUP
;
           LEA     DI,PROMPT           ;PROMPT FOR C_TEMP
           MOV     CX,42
           CALL    PUTSTRNG
           CALL    GETDEC              ;GET C_TEMP
           SUB     AX,32               ;F_TEMP = (9 * C_TEAM) / 5+32
           MOV     BX,5
           IMUL    BX
           MOV     BX,9
           IDIV    BX
           XCHG    AX,DX               ;REMAIN = (F_TEMP = (9 * C_TEAM) / 5+32
           MOV     BL,5                ;ROUND  = REMAIN / 5
           IDIV    BL
           CBW
           ADD     AX,DX               ;F_TEMP = F_TEMP + ROUND
;
           LEA     DI,ANNOTATION       ;DISPLAY F_TEMP
;          MOV     CX,42
           CALL    PUTSTRNG
           MOV     BH,0
           CALL    PUTDEC
           CALL    NEWLINE
           MOV     AH,4CH              ;RETURN TO DOS
           INT     21H
           END
;===================================================================
Title: Re: Help converting temperature assignment
Post by: Rsir on May 05, 2009, 02:47:52 PM
hi Peedi,
It would be helpfull if you also provide us with the code of your EXTRN's.
Is that possible?
grt Rsir
Title: Re: Help converting temperature assignment
Post by: FORTRANS on May 05, 2009, 03:12:47 PM
Hi,

   You should note that while the comments and strings were
updated, none of the working code changed.

Steve
Title: Re: Help converting temperature assignment
Post by: MichaelW on May 05, 2009, 04:52:25 PM
I wouldn't waste your time on this one, guys. The source is obviously a weak attempt, and in a post that I removed the OP was trying to buy help with a test, that was to be on April 30.
Title: Re: Help converting temperature assignment
Post by: vanjast on May 05, 2009, 08:44:32 PM
Peedi, you probably have to read through the 'instruction set' book, especially on the workings of each instruction.  :wink
It looks like you're making assumptions here... which is not a good thing when coding in asm.
Title: Re: Help converting temperature assignment
Post by: Rsir on May 07, 2009, 07:27:04 PM
Peedi,
MichaelW doesn't trust you.
Are you reliable?
Rsir
Title: Re: Help converting temperature assignment
Post by: MichaelW on May 08, 2009, 12:36:23 PM
QuoteMichaelW doesn't trust you.

Trust has nothing to do with it. This looks to me very much  like a lazy student trying to get someone else to do the work. The attempt failed and peedi departed, probably never to return.
Title: Re: Help converting temperature assignment
Post by: vanjast on May 09, 2009, 10:00:35 PM
Quote from: MichaelW on May 08, 2009, 12:36:23 PM
Trust has nothing to do with it. This looks to me very much  like a lazy student trying to get someone else to do the work.
Student studies can be quiet hectic, and they do get desperate.... Maybe a little help -  'letting one slip by' won't do any harm...
It does look like a simple project ?

Maybe it might be an idea to make a Student forum section... after all if a student doesn't ask.. they do not get help.
Even if it is somebody else's work, they'll still have to learn it, and the teachers/lecturers are great detectives when it comes to plaguerism

It'll also help promote Assembler if you 'gettim' young....
:wink
Title: Re: Help converting temperature assignment
Post by: Mark Jones on May 09, 2009, 10:59:44 PM
I sort-of agree, and have been saying for years that we should be actively looking to "recruit" new members.

But alas, learning assembler is a demanding task, that can only be done either by those who have an oustanding memory capacity and moderate drive to learn, or those (like me) who have a shoddy memory but are eternally determined to learn. :bg
Title: Re: Help converting temperature assignment
Post by: MichaelW on May 09, 2009, 11:49:48 PM
I have nothing in general against helping students, but I am not willing to waste my time trying to help someone who appears to me to be making no significant effort. Students who can learn a subject with no significant effort typically don't need help with the subject.

And since this is my second post in this thread after I decided not to waste time here, I guess I should point out the obvious "show stopper" error in the code, where it fails to initialize DS to the data segment.