1. how to shift bits in memory
- Posted by gareth_mcdaid Jan 24, 2011
- 1325 views
Hi everyone
I need to know how to shift bits in memory, I’m using version 3.1 of euphoria.
I’ve look in the manual for a inbuilt function to shift bits around in memory, but not been able to find one.
Do I need to write my own?
include misc.e include machine.e --The pic12f675 puts(1," pic12f675 \n") constant rom_size = 1024 --each opcode is incoded with 14 bits. --wraping is used because the program counter --is able to address upto 8kb constant ram_size = 256 --the ram is 256 bytes it is split into two banks --each bank is 128 bytes. --the general purpose ram is mapped across both banks --and starts at location 20h-5fh or 32-96 --constant eeprom_size = 128 atom rom atom ram sequence stack integer pc -- program counter integer regw -- working registery integer bank -- selected bank procedure init() pc = 0 regw = 0 bank = 0 stack={} rom = allocate(rom_size*2) ram = allocate(ram_size) end procedure procedure deinit() free(rom) free(ram) end procedure procedure reset() free(rom) free(ram) rom = allocate(rom_size*2) ram = allocate(ram_size) pc = 0 regw = 0 bank = 0 stack= {} end procedure procedure loadcart() integer fn,char,pos fn = open("data.bin","rb") for i=0 to 63 do char=getc(fn) poke(ram+32+i , char) end for pos=0 for i=0 to (rom_size/2)-1 do char=getc(fn) poke(rom+pos,char) char=getc(fn) poke(rom+pos+1,char) pos+=2 end for close(fn) end procedure function peek2(atom address) return peek(address)*256 + peek(address+1) end function integer instr integer optype integer opcode procedure execute_op() --?peek(rom) --?peek(rom+1) instr = peek2(rom+pc) instr = and_bits(instr,#3FFF) -- trim of the 2 msbits optype = and_bits(instr,#3000) ?instr ?optype if optype=0 then -- BYTE-ORIENTED FILE REGISTER OPERATIONS opcode = and_bits(instr,#F00) if opcode=8 then puts(1,"movf\n") end if ?opcode elsif optype=1 then -- BIT-ORIENTED FILE REGISTER OPERATIONS else -- LITERAL AND CONTROL OPERATIONS end if pc+=2 end procedure init() loadcart() execute_op() sleep(1)
2. Re: how to shift bits in memory
- Posted by gareth_mcdaid Jan 24, 2011
- 1295 views
would this shift 8 bits to the right? \ 256
opcode = and_bits(instr,#F00) \256
in c its esay to do
int value;
value = 4096>>8;
3. Re: how to shift bits in memory
- Posted by mattlewis (admin) Jan 24, 2011
- 1301 views
Hi everyone
I need to know how to shift bits in memory, I’m using version 3.1 of euphoria.
I’ve look in the manual for a inbuilt function to shift bits around in memory, but not been able to find one.
Do I need to write my own?
You might try Bit Operations by Juergen Luethje.
Matt
5. Re: how to shift bits in memory
- Posted by unkmar Jan 29, 2011
- 1206 views
I don't know what all is in that zip file.
I do know that bit-shifts are nothing more than multiply or of integers of powers of 2 in euphoria.
These are not bit rotations. The left does not rotate to the right.
integer test = 6 -- 8-bits of binary 00000110 test *= 2 -- left bit shift by 1, 00001100 test *= 4 -- left bit shift by 2, 00110000 test /= power(2, 3) -- power(2,3) = 8, right bit shift by 3, 00000011 test /= 2 -- right bit shift by 1, 00000001
6. Re: how to shift bits in memory
- Posted by DerekParnell (admin) Jan 29, 2011
- 1185 views
I don't know what all is in that zip file.
I do know that bit-shifts are nothing more than multiply or of integers of powers of 2 in euphoria.
These are not bit rotations. The left does not rotate to the right.
integer test = 6 -- 8-bits of binary 00000110 test *= 2 -- left bit shift by 1, 00001100 test *= 4 -- left bit shift by 2, 00110000 test /= power(2, 3) -- power(2,3) = 8, right bit shift by 3, 00000011 test /= 2 -- right bit shift by 1, 00000001
It is not quite that simple when 'shifting' negative numbers.
For example, the number -1 has the bit pattern 11111111_11111111_11111111_11111111 and if you wish to shift that two bits to the right you can't simply multiply it by 4.
-4 is 11111111_11111111_11111111_11111100 -1 >> 2 is 10111111_11111111_11111111_11111111