C ++ to pep / 8汇编语言帮助(链接数据结构)

时间:2013-03-23 19:47:11

标签: c++ assembly

对于我的一个CSCI课程,我必须将这个C ++程序翻译成pep / 8中的程序集:

#include <iostream>
using namespace std;

struct node {
int data;
node* next;
};

int main() { 
node *first, *p;
int value, sum;
first = 0;
cin>>value;
while (value != -9999) {
p =first;
first = new node;
first->data = value;
first->next = p;
cin>>value;
}

for (p=first;p != 0; p = p->next) {
cout<<p->data<<' ';
}

sum = 0;
p = first;
while (p != 0) {
sum += p->data;
p = p->next;
}

cout <<"Sum: "<<sum<<endl;
return 0;
}

到目前为止,这是我对汇编翻译的内容:

BR       main
data:    .EQUATE 0
next:    .EQUATE 2
;
;*********main()
first:   .EQUATE 6           ;local variables
p:       .EQUATE 4                     
value:   .EQUATE 2
sum:     .EQUATE 0

main:    SUBSP 8, i          
         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
         STA p, s            ;p=first
         LDA 4, i            ;first = new node
         CALL new
         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:  LDA 0, i
         STA sum, s          ;sum = 0
         LDA first, s        ;p = first
         STA p, s
while1:  LDA p, s            ;(while p != 0) 
         CPA 0, i
         BREQ endWh2
         LDX data, i          ;sum += p->data
         LDA p, sxf
         ADDA data, sxf                 
         STA sum, s        
         LDX next, i         ;p = p->next;
         LDA p, sxf
         STA p, s
         BR while1
endWh2:  LDA sum, s
         STRO msg1, d        ;"Sum = " 
         DECO sum, s         ;<<sum
         CHARO '\n', i       ;<<endl
         ADDSP 8, i
         STOP

msg1:    .ASCII "Sum: \x00"                          

;
;*******operator new
new:     LDX hpPtr, d
         ADDA hpPtr, d
         STA hpPtr, d
         RET0
hpPtr:   .ADDRSS heap
heap:    .BLOCK 1
         .END

我无法获得

sum += p->data

在装配中工作,

     LDX data, i          
     LDA p, sxf
     ADDA data, sxf                 
     STA sum, s 

我认为我在使用寻址模式时遇到了麻烦......

1 个答案:

答案 0 :(得分:0)

我在PEP / 8上找不到很多好的信息,所以这在黑暗中有点儿,但是因为到目前为止还没有其他答案我会试一试:

在我看来,你不是在计算

sum = p->data + sum

但是

sum = p>data + some_weird_things_that's_improperly_addressed

对我而言,它似乎应该是

 LDX data, i          
 LDA p, sxf   //load p->data into regA
 ADDA sum, s  //add sum to regA, regA now is p->data + sum               
 STA sum, s   //store regA back to sum