StackTest Problem

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

StackTest Problem

firgunner
This post was updated on .
So all the other tests pass except StackTest.

I have gone through each RAM location referenced in StackTest.out and it lines up perfectly with the state of my memory upon termination of the StackTest.asm, yet the test fails.

In addition to that, I have gone through each outputted command and written down the expected result beforehand and found that every command correctly satisfies my expectation of them.

So I have two main questions:

How does the test fail comparison when StackTest.out matches my memory state exactly. Does the test script examine more than what is output to StackTest.out?

Secondly, I have begun to doubt my understanding what the commands should do. So I have the following questions:

Whenever two operands are involved I assume where it matters say in x - y for the sub-command that x is *(SP-2) and y is *(SP-1) or something like that. Or is it the opposite way around? I assume whatever the convention is stays the same for all commands (where it matters of course). I chose that way since when implementing the high-level language we will be pushing onto the stack left-to-right (generally). Is this the wrong assumption.

I am beginning to wonder whether and, or, and not are bitwise or logical I implemented them as logical (and according to my thorough examination or StackTest.asm (stepping through each line) they work by representing true as 1 and false as 0. Do I have any misunderstandings here?

(EDIT)

So I took a look at StackTest.cmp I wasn't aware that was the correct file. My bad. I didn't pay attention to that one video about testing. Anyway, it looks like not is not logical but bitwise and true should be represented as -1 not 1? Is that correct?


(EDIT)

I figured it all out except for one thing. in StackTest.cmp the result of a not operation is -91. How can this be? My result is 0 since the operation in the test acts on 1 as a result of the previous or. I am moving on with the course for now since I feel like this is a small detail that I have missed or something but it is still bothering me so I would appreciate any answers and help.

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: StackTest Problem

ivant
firgunner wrote
I figured it all out except for one thing. in StackTest.cmp the result of a not operation is -91. How can this be? My result is 0 since the operation in the test acts on 1 as a result of the previous or. I am moving on with the course for now since I feel like this is a small detail that I have missed or something but it is still bothering me so I would appreciate any answers and help.

Here is the relevant part of the test and I put the contents of the stack after each operation:

push constant 57
// 57
push constant 31
// 31 57
push constant 53
// 53 31 57
add
// 84 57
push constant 112
// 112 84 57
sub
// -28 57
neg
// 28 57
// or in binary (and I'll use just 8 bits because the rest are just a copy of the leftmost one
// 00011000 00111001
and
// 00011000
push constant 82
// 01010010 00011000
or
// 01011010
not
// 10100101
The last number on the stack is negative, because it's sign bit is 1. To find it's value, we can convert it to positive (NOT, then +1), convert it to decimal and then change its sign. So the NOT part we already have: 01011010. +1 gives us 01011011. Convert to decimal: 1 + 2 + 8 + 16 + 64 = 91. Change sign: -91.

Reply | Threaded
Open this post in threaded view
|

Re: StackTest Problem

firgunner
This post was updated on .
Thank you, so it is bitwise NOT not logical NOT. That's where my confusion was. Do you know the reason for AND and OR begin logical whereas NOT is bitwise? I can't see understand why I would've/should've come to that conclusion. Is there something in the book that explains that? Should I buy the book? Are they all bitwise where is this distinction made?

(EDIT)

Wow so I am just a total noob. I thought AND OR and NOT had to switch from current state to either 0 if not zero or -1 if 0 (Similar to the comparison operators).

Now I am really happy the outputted assembly is much shorter and elegant. Thank you so much for walking me through that. Appreciate the help.
Reply | Threaded
Open this post in threaded view
|

Re: StackTest Problem

ivant
I'm glad I could help. Also, we all make mistakes like this. You can read a nice way to catch them here: https://blog.codinghorror.com/rubber-duck-problem-solving/. If you don't know him, the author is one of the 2 co-founders and developers of StackOverflow.
Reply | Threaded
Open this post in threaded view
|

Re: StackTest Problem

redemptionc
This post was updated on .
In reply to this post by firgunner
I had the same thought,too.
But I'm probably mislead by the slides: it says and/or/not return boolean
So I thought they are logical operators,and they only generate true or false,which is -1 or 0


Edit:
I read the book and found it clearly says and or not should do bitwise 😭 you shouldn’t only rely on slides