and/neg operations

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

and/neg operations

ngdrummer
So I'm working on my VM translator for project 7, and getting through the SimpleAdd was a breeze, but now that I'm trying the stack test, I'm running into an interesting error. I'm getting an error from the CPU Emulator saying "In line 274, Expression Expected", so I did some digging.

In the StackTest.vm, the following lines occur:

push constant 57
push constant 31
push constant 53
add
push constant 112
sub
neg
and

but doesnt neg just give the negative value of the current constant? or am I misunderstanding what neg really means? I'm confused because and seems like an operation that can only be executed on true or false, as I have it set up to give, however, with non 0 or -1 constants, how does and work?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

cadet1620
Administrator
ngdrummer wrote
So I'm working on my VM translator for project 7, and getting through the SimpleAdd was a breeze, but now that I'm trying the stack test, I'm running into an interesting error. I'm getting an error from the CPU Emulator saying "In line 274, Expression Expected", so I did some digging.
The CPU Emulator reads the ASM your translator generates. The error you are getting is because of a syntax problem in that ASM.
In the StackTest.vm, the following lines occur:

push constant 57
push constant 31
push constant 53
add
push constant 112
sub
neg
and

but doesnt neg just give the negative value of the current constant? or am I misunderstanding what neg really means? I'm confused because and seems like an operation that can only be executed on true or false, as I have it set up to give, however, with non 0 or -1 constants, how does and work?
The neg command is arithmetic negation.

And, or and not commands are bit-wise logical operations. That is, each individual bit in the word(s) are anded, ored, or notted.

The commands
    push 255  // 0x00FF
    not
result in -256 (0xFF00) on the top of the stack.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

ngdrummer
Ok, so it operates as I thought it would, except how do I get an integer negative of the current value? I tried
M=0-M
and I think that's where my syntax error is happening, can you help me?
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

ngdrummer
and and or are also confusing me when confronted with integer values.

how do i generalize how to "and" or "or" two integer values if not given them up front>?
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

cadet1620
Administrator
This post was updated on .
In reply to this post by ngdrummer
ngdrummer wrote
Ok, so it operates as I thought it would, except how do I get an integer negative of the current value? I tried
M=0-M
and I think that's where my syntax error is happening, can you help me?
M=-M
and and or are also confusing me when confronted with integer values.

how do i generalize how to "and" or "or" two integer values if not given them up front>?
The ALU's "and" and "or" operations are bit-wise and work just fine on integers.

An example of when you might want to bitwise-and and -or an integer is a trick I use in my minimal length Fill.asm to avoid having to compare and reset my screen pointer:
    p = 0x4000 // SCREEN
    forever {
        if key down
            *p = -1
        else
            *p = 0
        p = ((p+1) & 0x1FFF) | 0x4000
    }
I use bit-wise operations like this quite a bit when writing embedded system firmware.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

ngdrummer
ok, I get that the bitwise operators work fine, but I've never had to program bitwise operations in a high level language, so the notation #x#### is foreign to me, and I'm just trying to figure out how to represent the and operation or the or or not in assembly language, or high level language
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

ngdrummer
I guess when it comes down to it I'm completely lost on how to do bitwise operations in assembly language...
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

ngdrummer
OMG, & and | are actual symbols...I thought I was expected to write out arithmetic functions...life is awesome...wow
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

cadet1620
Administrator
In reply to this post by ngdrummer
ASM operations for bit-wise and and or are, for example, D=D&M and D=D|M.

0x12AB is hexadecimal notation which is a compact way of writing binary numbers. The digits are 0-9 and A-F which represent the binary values 0000-1001 and 1010-1111 respectively. Sometimes it's written 12ABh. The A-F and H and X can be written in either upper or lower case; it means the same thing.

In my example, 0x4000 = 0100000000000000 = 16384 = SCREEN and 0x1FFF = 0001111111111111.

Here's how the screen pointer "p = ((p+1) & 0x1FFF) | 0x4000" statement works:
The screen addresses are 0x4000 - 0x5FFF (0100000000000000 - 0101111111111111)
when when p+1 becomes 0x6000 -- the first address beyond the screen -- then:

0110000000000000 (p+1)
0001111111111111 AND
------------------
0000000000000000
0100000000000000 OR
------------------
0100000000000000 new p value: the beginning of screen address

when p+1 is still withing the screen address range:

0101001110101001 (p+1)
0001111111111111 AND
------------------
0001001110101001
0100000000000000 OR
------------------
0101001110101001 new p value: (p+1)

And can be thought of as selecting bits that we want to allow to be ones, and Or can be thought of as setting bits that we need to be on.

--Mark



Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

ngdrummer
Thanks for all the help, I'm just totally dumb and forgot I was able to use & and | in assembly. I spent all this time trying to figure out a way to write in +,-,JMP operations a bitwise and on integer values...

well, now that that's overwith, I'm stuck on this and that for push and pop in memoryvalues, hehe.
Reply | Threaded
Open this post in threaded view
|

Re: and/neg operations

linuxford
I almost missed the trees from the forest were it not for this thread. Thank you ngdrummer for the post and Mark once again.