Assembly Language for Beginners

(Jeff_L) #1

3.4 Fibonacci numbers.


_fahr$ = -8 ; size = 8
_main PROC
sub esp, 8
push esi
mov esi, DWORD PTR impprintf
push OFFSET $SG4228 ; 'Enter temperature in Fahrenheit:'
call esi ; call printf()
lea eax, DWORD PTR _fahr$[esp+16]
push eax
push OFFSET $SG4230 ; '%lf'
call DWORD PTR impscanf
add esp, 12
cmp eax, 1
je SHORT $LN2@main
push OFFSET $SG4231 ; 'Error while parsing your input'
call esi ; call printf()
add esp, 4
push 0
call DWORD PTR impexit
$LN9@main:
$LN2@main:
movsd xmm1, QWORD PTR _fahr$[esp+12]
subsd xmm1, QWORD PTR real@4040000000000000 ; 32
movsd xmm0, QWORD PTR
real@c071100000000000 ; -273
mulsd xmm1, QWORD PTR real@4014000000000000 ; 5
divsd xmm1, QWORD PTR
real@4022000000000000 ; 9
comisd xmm0, xmm1
jbe SHORT $LN1@main
push OFFSET $SG4233 ; 'Error: incorrect temperature!'
call esi ; call printf()
add esp, 4
push 0
call DWORD PTR impexit
$LN10@main:
$LN1@main:
sub esp, 8
movsd QWORD PTR [esp], xmm1
push OFFSET $SG4234 ; 'Celsius: %lf'
call esi ; call printf()
add esp, 12
; return 0 - by C99 standard
xor eax, eax
pop esi
add esp, 8
ret 0
$LN8@main:
_main ENDP


Of course,SIMDinstructions are available in x86 mode, including those working with floating point num-
bers.


It’s somewhat easier to use them for calculations, so the new Microsoft compiler uses them.


We can also see that the− 273 value is loaded intoXMM0register too early. And that’s OK, because the
compiler may emit instructions not in the order they are in the source code.


3.4 Fibonacci numbers


Another widespread example used in programming textbooks is a recursive function that generates the
Fibonacci numbers^3. The sequence is very simple: each consecutive number is the sum of the previous
two. The first two numbers are 0 and 1, or 1 and 1.


The sequence starts like this:


0 ; 1 ; 1 ; 2 ; 3 ; 5 ; 8 ; 13 ; 21 ; 34 ; 55 ; 89 ; 144 ; 233 ; 377 ; 610 ; 987 ; 1597 ; 2584 ; 4181 :::

(^3) http://go.yurichev.com/17332

Free download pdf