Assembly Language for Beginners

(nextflipdebug2) #1

3.3. TEMPERATURE CONVERTING


$SG4041 DB 'Error while parsing your input', 0aH, 00H
$SG4043 DB 'Error: incorrect temperature!', 0aH, 00H
$SG4044 DB 'Celsius: %lf', 0aH, 00H


real@c071100000000000 DQ 0c071100000000000r ; -273
real@4022000000000000 DQ 04022000000000000r ; 9
real@4014000000000000 DQ 04014000000000000r ; 5
real@4040000000000000 DQ 04040000000000000r ; 32


_fahr$ = -8 ; size = 8
_main PROC
sub esp, 8
push esi
mov esi, DWORD PTR impprintf
push OFFSET $SG4038 ; 'Enter temperature in Fahrenheit:'
call esi ; call printf()
lea eax, DWORD PTR _fahr$[esp+16]
push eax
push OFFSET $SG4040 ; '%lf'
call DWORD PTR impscanf
add esp, 12
cmp eax, 1
je SHORT $LN2@main
push OFFSET $SG4041 ; 'Error while parsing your input'
call esi ; call printf()
add esp, 4
push 0
call DWORD PTR impexit
$LN2@main:
fld QWORD PTR _fahr$[esp+12]
fsub QWORD PTR real@4040000000000000 ; 32
fmul QWORD PTR
real@4014000000000000 ; 5
fdiv QWORD PTR real@4022000000000000 ; 9
fld QWORD PTR
real@c071100000000000 ; -273
fcomp ST(1)
fnstsw ax
test ah, 65 ; 00000041H
jne SHORT $LN1@main
push OFFSET $SG4043 ; 'Error: incorrect temperature!'
fstp ST(0)
call esi ; call printf()
add esp, 4
push 0
call DWORD PTR impexit
$LN1@main:
sub esp, 8
fstp QWORD PTR [esp]
push OFFSET $SG4044 ; 'Celsius: %lf'
call esi
add esp, 12
; return 0 - by C99 standard
xor eax, eax
pop esi
add esp, 8
ret 0
$LN10@main:
_main ENDP


...but MSVC 2012 usesSIMDinstructions instead:


Listing 3.3: Optimizing MSVC 2010 x86

$SG4228 DB 'Enter temperature in Fahrenheit:', 0aH, 00H
$SG4230 DB '%lf', 00H
$SG4231 DB 'Error while parsing your input', 0aH, 00H
$SG4233 DB 'Error: incorrect temperature!', 0aH, 00H
$SG4234 DB 'Celsius: %lf', 0aH, 00H
real@c071100000000000 DQ 0c071100000000000r ; -273
real@4040000000000000 DQ 04040000000000000r ; 32
real@4022000000000000 DQ 04022000000000000r ; 9
real@4014000000000000 DQ 04014000000000000r ; 5

Free download pdf