2017-05-08 4 views
-1

ローカル変数またはグローバル変数として変数を宣言するために、アセンブリ言語でコードを理解するのに役立つ必要があります。2つのアセンブリコードの違い

は、このコード

;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:
"first"、 "p"、 "value"は、アセンブラのエイリアスです(どの変数を知るかは、変数に6バイトを予約しています) )、#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」、「第1」、コードに直接変数を追加しては、 "、" value "はラベルです。このコードはより長いです(変数はコードセグメントの一部です)。これは、プログラムがメモリ内で実行されている場合(変数は変更可能でなければならないため)、動作しません。変数のためのスペースがコード内にあるので、あなたは彼らが「実行」していないことを確認するためにそれらの上にジャンプする必要があります。
V1: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に注意してください。
v2:コードはないが別名のみが定義されているため、「BRメイン」は不要です