Last active
November 2, 2024 16:54
-
-
Save cyjico/9124649fd59d3cfcfa790cb54ffface3 to your computer and use it in GitHub Desktop.
Requires Microsoft Macro Assembler (MASM)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.386 ; compiled for Intel 80386 or later and 32-bit instructions and addressing should be used. | |
.model flat,stdcall ; memory is a single contiguous address space and use standard calling convention of procedures. | |
.stack 4096 ; reserve 4096 bytes for the "stack". | |
ExitProcess PROTO dwExitCode:DWORD ; prototype of "ExitProcess" procedure has the parameter of a DWORD "dWExitCode". | |
.data | |
; <variable_name> <variable_type> <variable_value> | |
farenheit REAL4 170.0 ; REAL4 is a 4-byte (32-bits) floating-point number. | |
THIRTY_TWO REAL4 32.0 | |
FIVE REAL4 5.0 | |
NINE REAL4 9.0 | |
celcius REAL4 ? ; ? is uninitilized and can contain anything which was there or not there previously (garbage value). | |
FEVERISH_CUT_OFF REAL4 37.5 | |
result DWORD ? ; DWORD is a 4-byte (32-bits) unsigned integer. it stands for DOUBLE WORD because a WORD is 16-bits. | |
.code | |
main PROC ; marks the start of the procedure named "main". <procedure_name> PROC | |
FINIT ; initialize FPU. | |
; step 1. convert farenheit to celcius | |
FLD farenheit ; load value of "farenheit". | |
FSUB THIRTY_TWO ; subtract 32 from FPU. | |
FMUL FIVE ; multiply FPU by 5. | |
FDIV NINE ; divide FPU by 9. | |
FSTP celcius ; store FPU into "celcius". | |
; step 2. determine if the temperature is feverish. | |
mov result,1 ; assume feverish | |
FLD celcius ; reload "celcius" into the FPU. remember, FSTP is destructive, destroying the FPU stack after storing. | |
FCOM FEVERISH_CUT_OFF ; compare FPU with the variable "FEVERISH_CUT_OFF". | |
; by setting the FLAGS register, we can use conditional jumps like JAE (Jump if Above or Equal). | |
FSTSW AX ; store FPU status word into the AX (16-bit accumulator) register. | |
SAHF ; store AH (high byte of the AX register) into the lower byte of the FLAGS register. | |
JAE hasFever ; if above or equal, jump to the label "hasFever". | |
mov result,0 ; else, we assumed wrong and load 0 into result. | |
hasFever: ; labels a location we can jump to. <label_name>: | |
INVOKE ExitProcess,0 ; invoke is a high-level macro provided to simplify calling and procedures. | |
main ENDP ; marks the end of procedure named "main". <procedure_name> ENDP | |
END main ; end of the source file and specifies that the procedure "main" is the entry point. END <entry_point_name> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment