I need help understanding a code in assembly language, in terms of declaring variables as local or global variables.
What is the difference between this code
;File: fig0647.pep
;Computer Systems, Fourth edition
;Figure 6.47
;
BR main
data: .EQUATE 0 ;struct field #2d
next: .EQUATE 2 ;struct field #2h
;
;******* main ()
first: .EQUATE 4 ;local variable #2h
p: .EQUATE 2 ;local variable #2h
value: .EQUATE 0 ;local variable #2d
main: SUBSP 6,i ;allocate #first #p #value
LDA 0,i ;first = 0
STA first,s
DECI value,s ;cin >> value
while: LDA value,s ;while (value != -9999)
CPA -9999,i
BREQ endWh
LDA first,s ; p = first
STA p,s
LDA 4,i ; first = new node
CALL new ; allocate #data #next
STX first,s
LDA value,s ; first->data = value
LDX data,i
STA first,sxf
LDA p,s ; first->next = p
LDX next,i
STA first,sxf
DECI value,s ; cin >> value
BR while
endWh: LDA first,s ;for (p = first
STA p,s
for: LDA p,s ; p != 0
CPA 0,i
BREQ endFor
LDX data,i ; cout << p->data
DECO p,sxf
CHARO ' ',i ; << ' '
LDX next,i ; p = p->next)
LDA p,sxf
STA p,s
BR for
endFor: ADDSP 6,i ;deallocate #value #p #first
STOP
;
;******* operator new
; Precondition: A contains number of bytes
; Postcondition: X contains pointer to bytes
new: LDX hpPtr,d ;returned pointer
ADDA hpPtr,d ;allocate from heap
STA hpPtr,d ;update hpPtr
RET0
hpPtr: .ADDRSS heap ;address of next free byte
heap: .BLOCK 1 ;first byte in the heap
.END
And this code
BR main
data: .EQUATE 0 ;struct field #2d
next: .EQUATE 2 ;struct field #2h
;
;.........main()
first: .BLOCK 2 ;global variable #2h
p: .BLOCK 2 ;global variable #2h
value: .BLOCK 2 ;global variable #2d
main: LDA 0,i ;first = 0
STA first,s
DECI value,s ;cin >> value
while: LDA value,s ;while (value != -9999)
CPA -9999,i
BREQ endWh
LDA first,s ; p = first
STA p,s
LDA 4,i ; first = new node
CALL new ; allocate #data #next
STX first,s
LDA value,s ; first->data = value
LDX data,i
STA first,sxf
LDA p,s ; first->next = p
LDX next,i
STA first,sxf
DECI value,s ; cin >> value
BR while
endWh: LDA first,s ;for (p=first)
STA p,s
for: LDA p,s ; p != 0
CPA 0,i
BREQ endFor
LDX data,i ; couunt << p->data
DECO p,sxf
CHARO ' ',s ; << ' '
LDX next,s ; p = p->next)
LDA p,sxf
STA p,s
BR for
endFor: STOP
;
;....... operator new
; Precondition: A contains number of bytes
; Postcondition: X contains pointer to byte
new: LDX hpPtr,d ;returned pointer
ADDA hpPtr,d ;allocate from heap
STA hpPtr,d ;update hpPtr
RET0
hpPtr: .ADDRSS heap ;address of next free byte
heap: .BLOCK 1 ;first byte in the heap
.END
Any kind of help would be appreciated.
Variant 1:
Adds 6 bytes to some stack pointer at startup (which reserves 6 bytes for the variables), "first","p","value" just are aliases for the assembler ( to know which variable should be stored, the same thing that
#define first 2would do in C ), and there is no code generated for these.EquateVariant2:
is adding the variables directly into the code, "first", "p", "value" are labels. This code is longer (since the variables are somewhat part of the code segment). This only works, if the program is running in memory (since the variables must be changeable) and wouldn't work e.g. in a ROM
Also note the BR main:
v1: Since the space for the variables is inside the code, you have to jump over them to make sure they're not "executed".
v2: the "BR main" is not needed, since there is no code, but only alias defines