C ++到pep / 8程序集:反转链表

时间:2013-04-01 05:33:23

标签: c++ assembly linked-list

对于我的一个CSCI类,我必须将这个C ++程序翻译成pep / 8 cpu架构中的汇编程序

#include <iostream>
using namespace std;

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

void reverse (node* list) {
if (list != 0) {
reverse (list->next);
cout<<list->data << ' ';
}
}


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<<' ';
}

cout<<endl;
reverse(first);
return 0;
}

现在我已将这些内容翻译成汇编:

BR       main
data:    .EQUATE 0
next:    .EQUATE 2
;

;********void reverse(node* list)

list:    .EQUATE 0
retVal:  .EQUATE 2

reverse: SUBSP 2, i          
     LDA list, s         ;if (list != 0) 
     CPA 0, i
     BREQ endIf
     LDX next, i         ;push list->next
     LDA list, sxf
     STA -2, s
     SUBSP 2, i
     CALL reverse        ;reverse(list->next)
     ADDSP 2, i
     LDX data, i         ;cout<<list->data
     DECO list, sxf
     CHARO ' ', i        ;cout<<' '
     RET2
endIf:   RET2





;*********main()
first:   .EQUATE 4           ;local variables
p:       .EQUATE 2                     
value:   .EQUATE 0


main:    SUBSP 6, 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:  CHARO '\n', i       ;cout<<endl
     LDA first, s
     STA -2, s           ;LOOK HERE
     SUBSP 2, i
     CALL reverse
     ADDSP 2, i
     ADDSP 6, i
     STOP


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

该程序以正向和反向顺序输出列表。

我遇到了反向方法的问题。我想可能就在这一部分:

endFor:  CHARO '\n', i       ;cout<<endl
 LDA first, s
 STA -2, s           ;LOOK HERE
 SUBSP 2, i
 CALL reverse
 ADDSP 2, i
 ADDSP 6, i
 STOP

抱歉草率格式化。没有理由,我知道! 提前谢谢

0 个答案:

没有答案