2017-05-08 107 views
-1

我需要幫助理解彙編語言代碼,在聲明變量爲局部或全局變量的條款。差異之間的兩個彙編代碼

是這段代碼

;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     

而這種代碼

  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 

任何形式的幫助,將不勝感激的區別。

回答

1

變體1:
在啓動時爲某個堆棧指針添加6個字節(爲變量保留6個字節),「first」,「p」,「value」只是彙編器的別名(要知道哪個變量應貯存,同樣的事情,#define first 2將會用C做的),並沒有對這些.Equate

BR main      ; <-- this BR is unneeded, there's no code from here to 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 

VARIANT2生成的代碼:
直接添加變量到代碼中,「第一」,「p 「,」價值「是標籤。這段代碼更長(因爲變量是代碼段的一部分)。如果程序在內存中運行(因爲變量必須可更改)並且不起作用,這僅適用於在ROM

BR  main   ; this BR makes sense, there's code inbetween this and main 
first: .BLOCK 2   ;<-- here e.g. a "00 00" appears INSIDE the code 
p:  .BLOCK 2   ;global variable #2h  
value: .BLOCK 2   ;global variable #2d 
main: LDA  0,i   

還要注意BR主:
V1:由於對變量的空間裏面的代碼,你必須跳過它們,以確保他們沒有「執行」。
版:在「BR主」是不需要的,因爲沒有代碼,而只是別名定義