gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:11 24 4
gpt4 key购买 nike

对于我的一门 CSCI 类(class),我必须将此 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

assembly 线工作,

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

我想我在寻址模式或其他方面遇到了问题......

最佳答案

我找不到很多关于 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

关于C++ 到 pep/8 汇编语言帮助(链接数据结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15591313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com