【问题标题】:Difference between two assembly code两个汇编代码之间的区别
【发布时间】:2017-05-08 12:35:53
【问题描述】:

在将变量声明为局部变量或全局变量方面,我需要帮助理解汇编语言代码。

这段代码有什么区别

;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

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

【问题讨论】:

    标签: assembly pep8-assembly


    【解决方案1】:

    变体 1:
    在启动时向某个堆栈指针添加 6 个字节(为变量保留 6 个字节),"first","p","value" 只是汇编程序的别名(知道应该存储哪个变量,与 @ 相同) 987654321@ 将在 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
    

    变体2:
    就是将变量直接加到代码中,“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 主要:
    v1:由于变量的空间位于代码内部,因此您必须跳过它们以确保它们没有被“执行”。
    v2:不需要“BR main”,因为没有代码,只有别名定义

    【讨论】:

      猜你喜欢
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多