对于我的一个 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
我想我在寻址模式或其他方面有问题......