堆栈相对vs堆栈延迟寻址

时间:2012-11-27 02:57:16

标签: memory assembly pep8-assembly

下面两个代码示例中s和sf之间的实际差异是什么?

据我所知,堆栈相对看起来像Mem [SP + OprndSpec],并且看起来像Mem [Mem [SP + OprndSpec]]。然而,我不明白的是如何实现这一目标。

Stack deffered

         BR      main
a:       .BLOCK  2          ;global variable #2d
b:       .BLOCK  2          ;global variable #2d
;
;******* void swap (int& r, int& s)
r:       .EQUATE 6          ;formal parameter #2h
s:       .EQUATE 4          ;formal parameter #2h
temp:    .EQUATE 0          ;local variable #2d
swap:    SUBSP   2,i        ;allocate #temp
         LDA     r,sf       ;temp = r
         STA     temp,s
         LDA     s,sf       ;r = s
         STA     r,sf
         LDA     temp,s     ;s = temp
         STA     s,sf
         RET2               ;deallocate #temp, pop retAddr
;
;******* void order (int& x, int& y)
x:       .EQUATE 4          ;formal parameter #2h
y:       .EQUATE 2          ;formal parameter #2h
order:   LDA     x,sf       ;if (x > y)
         CPA     y,sf
         BRLE    endIf
         LDA     x,s        ;   push x
         STA     -2,s
         LDA     y,s        ;   push y
         STA     -4,s
         SUBSP   4,i        ;   push #r #s
         CALL    swap       ;   swap (x, y)
         ADDSP   4,i        ;   pop #s #r
endIf:   RET0               ;pop retAddr

;
;******* main ()
main:    STRO    msg1,d     ;cout << "Enter an integer: "
         DECI    a,d        ;cin >> a
         STRO    msg1,d     ;cout << "Enter an integer: "
         DECI    b,d        ;cin >> b
         LDA     a,i        ;push the address of a
         STA     -2,s
         LDA     b,i        ;push the address of b
         STA     -4,s
         SUBSP   4,i        ;push #x #y
         CALL    order      ;order (a, b)
ra1:     ADDSP   4,i        ;pop #y #x
         STRO    msg2,d     ;cout << "Ordered they are: "
         DECO    a,d        ;     << a
         STRO    msg3,d     ;     << ", "
         DECO    b,d        ;     << b
         CHARO   '\n',i     ;     << endl
         STOP
msg1:    .ASCII  "Enter an integer: \x00"
msg2:    .ASCII  "Ordered they are: \x00"
msg3:    .ASCII  ", \x00"
         .END   

堆叠相对

         BR      main        
;
;******* int binomCoeff (int n, int k)
retVal:  .EQUATE 10          ;returned value #2d
n:       .EQUATE 8           ;formal parameter #2d
k:       .EQUATE 6           ;formal parameter #2d
y1:      .EQUATE 2           ;local variable #2d
y2:      .EQUATE 0           ;local variable #2d
binCoeff:SUBSP   4,i         ;allocate #y1 #y2
if:      LDA     k,s         ;if ((k == 0)
         BREQ    then        
         LDA     n,s         ;|| (n == k))
         CPA     k,s         
         BRNE    else        
then:    LDA     1,i         ;return 1
         STA     retVal,s    
         RET4                ;deallocate #y2 #y1, pop retAddr
else:    LDA     n,s         ;push n - 1
         SUBA    1,i         
         STA     -4,s        
         LDA     k,s         ;push k
         STA     -6,s        
         SUBSP   6,i         ;push #retVal #n #k
         CALL    binCoeff    
ra2:     ADDSP   6,i         ;pop #k #n #retVal
         LDA     -2,s        ;y1 = binomCoeff (n - 1, k)
         STA     y1,s        
         LDA     n,s         ;push n - 1
         SUBA    1,i         
         STA     -4,s        
         LDA     k,s         ;push k - 1
         SUBA    1,i         
         STA     -6,s        
         SUBSP   6,i         ;push #retVal #n #k
         CALL    binCoeff    
ra3:     ADDSP   6,i         ;pop #k #n #retVal
         LDA     -2,s        ;y2 = binomCoeff (n - 1, k - 1)
         STA     y2,s        
         LDA     y1,s        ;return y1 + y2
         ADDA    y2,s        
         STA     retVal,s    
endIf:   RET4                ;deallocate #y2 #y1, pop retAddr
;
;******* main ()
main:    STRO    msg,d       ;cout << "binCoeff (3, 1) = "
         LDA     3,i         ;push 3
         STA     -4,s        
         LDA     1,i         ;push 1
         STA     -6,s        
         SUBSP   6,i         ;push #retVal #n #k
         CALL    binCoeff    
ra1:     ADDSP   6,i         ;pop #k #n #retVal
         DECO    -2,s        ;<< binCoeff (3, 1)
         CHARO   '\n',i      ;cout << endl
         STOP                
msg:     .ASCII  "binCoeff (3, 1) = \x00"
         .END                  

1 个答案:

答案 0 :(得分:1)

您的问题似乎很简单,除了可能包含示例之外,与代码几乎没有关系。

(我对这个指令集并不熟悉,但是在汇编时已经编写了很长时间。)

根据您提供的定义,“堆栈相对”意味着从堆栈指针确定的位置获取内存中的项目,加上可能嵌入在指令中的常量偏移量。这被大多数人称为索引寻址,特别注意它是堆栈指针索引。

“Deferred”(旧术语)通常表示“通过内存位置间接”,并且您对此的定义与此想法一致:找到“堆栈相对”位置,读取该位置,并将该值用作内存位置去取。