Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Updated 6-Sep-18 23:29pm

1 solution

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
but that could be completely wrong.
 
Share this answer
 
Comments
Member 13976376 7-Sep-18 5:40am    
Thank you for your reply. It really means a lot since I'm trying to wrap my head around this while feeling completely useless haha.

I get what you're saying about testing if it's zero to exit the loop before pushing, but after removing the PSH R0 and trying out some different places for it to be, and removing it completely nothing shows up in the stack at all. I guess the main problem I have is where to execute the push and pop commands.
OriginalGriff 7-Sep-18 5:47am    
You need two labels, and two "jump" instructions:

@PushLoop:
Get it.
Test it
If it's zero jump to @EndPushLoop
Push it
Count it
Jump to @PushLoop
@EndPushLoop:
// Carry on and print 'em!

Make sense?
Member 13976376 7-Sep-18 6:18am    
Once again, thank you very much for helping me understand.

I understand the concept of the Loops, and what they do. Basically if the input is 0, it jumps down to @EndPushLoop, and if not, it loops
@PushLoop so that it asks the user to input a value again.

The code I've got at the moment is this, but I'm missing out on "Test it" as you wrote, and also "count it". I also need to print the stack, but I think the main priority would be to get the push and pops correctly.

I'll post a screenshot of the Sim along with the code down below:

https://gyazo.com/d5a711a2e049c5390de713cd5987be41
OriginalGriff 7-Sep-18 6:27am    
No, I'm not doing it for you! :laugh:
You have the idea, so the implementation should be pretty simple: it may be that the CPY instruction sets the Z flag, so you can use something like JZ immediately after you read the value. Have a look in the description and see.
Member 13976376 7-Sep-18 6:34am    
Alright haha. I won't get any better by letting you complete it for me so I appreciate what you're saying. Thanky you for your input, I'll try to figure this out so that I hopefully can have a peaceful friday night haha.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900