Slide 13.1: Advanced procedures
Slide 13.3: .MODEL directive
Home

Local Variables (Cont.)


Local variables are created on the runtime stack. They can not be given default values at assembly time, but they can be initialized at runtime. The following examples calculate a geometric series:
      a×rn-1 + ... + a×r3 + a×r2 + a×r1 + a
    = 3×23 + 3×22 + 3×21 + 3
 Using Global Variables   Using Local Variables 
 INCLUDE Irvine32.inc
 a = 3
 r = 2
 n = 4         ; n > 1

 .data
     result  SDWORD  0
     count    DWORD  ?
     r1      SDWORD  ?
     prod    SDWORD  ?

 .code
 main PROC
     mov   ecx, n
     dec   ecx
 L1: mov   count, ecx
     call  calc
     

add result, eax loop L1 add result, a mov eax, result call WriteInt exit main ENDP ;; Calculate a×r**n calc PROC mov r1, r mov eax, a ;; EAX: multiplicand L1: imul r1 loop L1 mov ecx, count mov prod, eax ; EAX: product ret calc ENDP END main
 INCLUDE Irvine32.inc
 a = 3
 r = 2
 n = 4         ; n > 1

 .data
     count    DWORD  ?
     prod    SDWORD  ?

 .code
 main PROC
     LOCAL  var:SDWORD
     mov    var, 0
     mov    ecx, n
     dec    ecx
 L1: mov    count, ecx
     call   calc
     

add var, eax loop L1 add var, a mov eax, var call WriteInt exit main ENDP ;; Calculate a×r**n calc PROC LOCAL var:SDWORD mov var, r mov eax, a ;; EAX: multiplicand L1: imul var loop L1 mov ecx, count mov prod, eax ; EAX: product ret calc ENDP END main