tsoding / porth

It's like Forth but in Python
628 stars 50 forks source link

implement intrinsic load and store 64 bit values #54

Closed zrthxn closed 2 years ago

zrthxn commented 2 years ago

Fixes #51

zrthxn commented 2 years ago

The simulation runs fail because the memory is a bytearray and we can't add bigger size values to it. The following can be done to replace the existing python simulation...

            elif op.operand == Intrinsic.LOAD64:
                addr = stack.pop()
                for offset in range(0,7):
                    byte = mem[addr + offset]
                    stack.append(byte)
                ip += 1
            elif op.operand == Intrinsic.STORE64:
                store_value = stack.pop()
                store_addr = stack.pop()
                for offset in range(0,7):
                    mem[store_addr + offset] = store_value & 0xFF
                    store_value >> 8
                ip += 1

But the print keyword can' t handle 64 bit values in simulations, so the output isn't exactly as expected.

// print 1000...0001 (64 bit word)
mem 1 63 shl 
1 + .64
mem ,64 print

// print UINT64_MAX (64 bit word)
mem 18446744073709551615 .64 
mem ,64 print

This code should print the following

9223372036854775809
18446744073709551615

but it actually prints

1     // 9223372036854775809 & 0xFF
255  // 18446744073709551615 & 0xFF

While compiling, its perfectly fine

rexim commented 2 years ago

@zrthxn you may wanna look into https://docs.python.org/3/library/stdtypes.html#int.to_bytes

zrthxn commented 2 years ago

@rexim Thanks! Didn't think of that. It works now

rexim commented 2 years ago

@zrthxn looks good to me! :+1: Thank you for your contribution!