-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

任何形式的帮助将不胜感激。

4

1 回答 1

1

变体1:
在启动时向某个堆栈指针添加6个字节(为变量保留6个字节),“first”,“p”,“value”只是汇编程序的别名(知道应该存储哪个变量,相同在 C中会做的事情#define first 2),并且没有为这些生成代码.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:
是将变量直接添加到代码中,“first”、“p”、“value”是标签。这段代码更长(因为变量在某种程度上是代码段的一部分)。这仅适用于程序在内存中运行(因为变量必须是可变的)并且例如在 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 main:
v1:由于变量的空间在代码内部,因此您必须跳过它们以确保它们没有“执行”。
v2:不需要“BR main”,因为没有代码,只有别名定义

于 2017-05-08T13:10:13.453 回答