Cpu模拟器 - 推送和弹出帮助。 [英] Cpu simulator - push and pop help.

查看:114
本文介绍了Cpu模拟器 - 推送和弹出帮助。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用CpuSimulator,我正在努力完成下面的说明,所以一些帮助将不胜感激。



键入一个程序从IO(输入)读取数字,将它们写入IO并将它们保存在堆栈(PSH)中,直到用户输入数字0.然后以相反的顺序打印数字,与使用存储的数据相比如何输入数字(POP)。数字0不应该存储在堆栈中。确保像你推的那样弹出多次,否则你将遇到任务17(主程序和子程序)的问题。



我目前的代码如下:



@print:



CPY R0 IO



PSH RO



JNZ R0 @print



这就是我的到来,如果你能帮助我理清如何:我很感激你们这样做:



a)让它停在0而不显示0在堆栈中



b)如何/何时推送和弹出



c)如何将堆栈打印到输出日志



提前致谢。



我尝试过:



我已经编写了上面的代码,而且我无法继续进行。

I'm currently working with a CpuSimulator, and I'm struggling with the instructions below, so some help would be greatly appreciated.

"Type a program that reads numbers from IO (input), write them to IO and save them on the stack (PSH) until the user inputs the number 0. Then print the numbers in reverse order compared to how they were fed in by utilizing what is stored with (POP). The number 0 should not be stored on the stack. Be sure to Pop as many times like you pushed or else you will have problem with task 17 (main program and subroutines)."

My current code looks like this:

@print:

CPY R0 IO

PSH RO

JNZ R0 @print

This is as far as I've come, and I'd really appreciate if you could help me sort out how to:

a) get it to stop at 0 without the 0 showing up in the stack

b) how/when to push and pop

c) how to print out the stack to the output log

Thanks in advance.

What I have tried:

I've written the code above, and I've been unable to proceed any further.

推荐答案

a)很简单:在推送之前测试,而不是之后测试。此刻,你读了价值,然后决定重复。你需要阅读它,测试它,如果它是零退出循环,否则推,然后再循环。

b)我不熟悉汇编代码:所以我不能精确。但是......只有两种方法可以弹出尽可能多的项目:

b.1)在POP之前检查堆栈的底部。如果你在那里,它是空的,你不应该再持有POP。为了使其正常工作,你需要在第一次推送之前存储初始堆栈指针并检查它。

b.2)使用计数器让你知道你推了多少项。然后你可以关掉这个号码。



第二种方法可能更容易。

c)不知道什么,我怀疑你的CPU /汇编程序有点专门为你的功课!它会出现在老师给你的指导集中的某个地方。如果我不得不猜测,我会认为它类似于

a) Is simple: test before you push, not after. At the moment, you read the value, poush it, and then decide to repeat. You need to read it, test, it if it's zero exit the loop, otherwise push and go round again.
b) I'm not at all familiar with the assembly code: so I can't be precise. But ... there are only two ways to pop as many items as you push:
b.1) Check for bottom of stack before you POP. If you are there, it;s empty, and you shouldn't POP any more. To make that work properly, you would need to store the initial stack pointer before you first PUSH and check against that.
b.2) Use a counter so you know how many items you PUSHed. You can then POP that number off.

The second approach is probably easier.
c) No idea whatsoever, I suspect your CPU / assembler is a bit made up specifically for your homework! It'll be in the instruction set your teacher gave you, somewhere. If I had to guess, I'd assume it's something like
CPY   IO   R0

但这可能完全错误。


这篇关于Cpu模拟器 - 推送和弹出帮助。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆