Author Topic: Writing to bits  (Read 14377 times)

0 Members and 2 Guests are viewing this topic.

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Writing to bits
« on: November 08, 2010, 03:30:55 pm »
Whats the smallest way of writing bits? (I don't care if it's Axe or Asm(HEX) type of thing)

So far the only thing I've come up with is
Code: [Select]
{Pointer}±e^(7-Bit#)→{Pointer}Use + for writing 1 to the bit, or - for writing 0 to the bit. The only problem is that writing 0 or 1 to a bit that already contains 0 or 1 respectively, will mess everything up.

Does anyone have any ideas?
« Last Edit: November 08, 2010, 03:31:09 pm by squidgetx »

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Writing to bits
« Reply #1 on: November 08, 2010, 03:35:56 pm »
{Pointer} xor %0000 0100

will write the third bit to one. just replace the 1 with whichever bit you're trying to write, and convert the binary into a decmal number. remember, if you're writing a bit to a two-byte area, you need to use the plot-style token instead of xor.


Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Writing to bits
« Reply #2 on: November 08, 2010, 03:38:53 pm »
{Pointer} xor %0000 0100

will write the third bit to one. just replace the 1 with whichever bit you're trying to write, and convert the binary into a decmal number. remember, if you're writing a bit to a two-byte area, you need to use the plot-style token instead of xor.

Actually, xor will flip the 3rd bit back and forth.  If the 3rd bit equals zero, xor will make the 3rd bit equal to 1.  If it's 1, xor will make the 3rd bit equal to zero.

Try using {Pointer} and %11111011 to make the 3rd bit equal to zero, for example.  Try using {Pointer} or %00000100 to make the 3rd bit equal to one.

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Writing to bits
« Reply #3 on: November 08, 2010, 03:41:04 pm »
Thanks both of you!

This makes things a lot easier

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Writing to bits
« Reply #4 on: November 08, 2010, 03:42:01 pm »
{Pointer} xor %0000 0100

will write the third bit to one. just replace the 1 with whichever bit you're trying to write, and convert the binary into a decmal number. remember, if you're writing a bit to a two-byte area, you need to use the plot-style token instead of xor.

Actually, xor will flip the 3rd bit back and forth.  If the 3rd bit equals zero, xor will make the 3rd bit equal to 1.  If it's 1, xor will make the 3rd bit equal to zero.

Try using {Pointer} and %11111011 to make the 3rd bit equal to zero, for example.  Try using {Pointer} or %00000100 to make the 3rd bit equal to one.


oops. i forgot about that. of course, or will work too.
« Last Edit: November 08, 2010, 03:42:35 pm by nemo »


Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Writing to bits
« Reply #5 on: November 08, 2010, 03:44:03 pm »
Out of curosity, are you wanting to make a particular bit one or zero, or does the bit you want to edit change every time that section of code executes?

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Writing to bits
« Reply #6 on: November 08, 2010, 03:47:58 pm »
General form: {Pointer}Asm(CBXX)→{Pointer}, where XX is a byte from the following table. In this table, I call bit 0 the least significant bit and bit 7 the most significant bit.

RESET   SET
BIT 0     85C5
BIT 18DCD
BIT 295D5
BIT 39DDD
BIT 4A5E5
BIT 5ADED
BIT 6B5F5
BIT 7BDFD
« Last Edit: November 08, 2010, 03:52:27 pm by Runer112 »

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Writing to bits
« Reply #7 on: November 08, 2010, 03:51:15 pm »
Runer: Thanks, but I need a routine that will take the pointer and bit # as arguments, so I think Hot_Dog's {Pointer} or %00000100 will be sufficient :)

Hot_Dog; I'm using the bits as flags to show whether the player has completed a certain task or not, so I guess they need to be constant.

So I'm going to go with, if you were going to write it as a subroutine with r1 being the pointer and r2 being the bit number (in Axe's way, with 0 being the most significant), the best way to do this is {r1} or e^(7-r2)→{r1}

Or is it more efficient to write it out every time? (no subroutine calling) just tested it, I think subroutine is better (9 bytes per call I think) and I think the code is ~41 bytes
« Last Edit: November 08, 2010, 04:13:31 pm by squidgetx »

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Writing to bits
« Reply #8 on: November 08, 2010, 05:45:48 pm »
Here's the most optimized way, using standard subroutine calls, to either set or reset a bit of a pointed to byte, with bit 0 being the most significant bit. Input would be in the form of sub(SET/RES,bit,pointer).

Code: [Select]
Lbl SET
{} or sub(BIT)
Lbl SR2
→{r₂}
Return
Lbl RES
{} and (sub(BIT) xor ᴇFF)
Goto SR2
Lbl BIT
e^(7-r₁)
Return

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Writing to bits
« Reply #9 on: November 08, 2010, 05:48:42 pm »
Oh cool, thanks. So I'm guessing that the lack of an argument inside the {} is because it is taking HL as its argument, which is 'pointer' because when you store pointer to r2 it is stored to HL....like ans....kk i'm learning XD

Does every Axe operation leave stuff in HL?
« Last Edit: November 08, 2010, 05:50:05 pm by squidgetx »

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Writing to bits
« Reply #10 on: November 08, 2010, 05:52:55 pm »
Oh cool, thanks. So I'm guessing that the lack of an argument inside the {} is because it is taking HL as its argument, which is 'pointer' because when you store pointer to r2 it is stored to HL....like ans....kk i'm learning XD

Does every Axe operation leave stuff in HL?

Every loading or mathematical operation will put the result in hl. As I mentioned to someone a while ago in a different thread, the hl register is like the "Ans of Axe." All loading operations put the value into hl, and all mathematical operations put the operands into hl and de, calculate, and put the result back into hl. The nice thing about this method is that complex strings of operations can be handled easily. If parentheses or curly brackets are reached, the current value of hl can just be pushed onto the stack for later, and any operations in the parentheses or curly brackets can then be handled using hl. Every time the "depth" of the operation is increased, hl just needs to be pushed, and every time the "depth" decreases, hl can be popped right back.

Note: Just in case anyone reading this doesn't know what pushing and popping are, they are methods of saving and restoring data (respectively). Pushing loads a value onto the top of a stack of values, like a stack of physical objects. Popping takes the top value on the stack and retrieves that. Using this method, you can push multiple values onto the stack and retrieve them in the opposite order that you put them there, allowing things like parentheses to be handled very well. It's explained quite well here.
« Last Edit: November 08, 2010, 06:08:55 pm by Runer112 »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Writing to bits
« Reply #11 on: November 09, 2010, 01:48:42 am »
Thanks for explaining push/pop. I always wondered what it did, but knew it involved the stack. That might help me a bit when learning ASM.

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: Writing to bits
« Reply #12 on: November 09, 2010, 03:00:42 pm »
Wait!? DJ, you don't know ASM!?!? I've only been at it 2 weeks and I have almost completely remade Hexpipc (again, but this time it's only 500 bytes). Seriously though, I thought you (being the founder of this forum) would know ASM.
« Last Edit: November 09, 2010, 03:08:32 pm by Binder News »
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Writing to bits
« Reply #13 on: November 09, 2010, 03:38:54 pm »
I think the language you know doesn't have any relation to how well you program or how fun you can make your games :) In fact many of the other Admins have little or no asm experience (myself included) and yet ive seen them make incredible games as well ^^

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Writing to bits
« Reply #14 on: November 09, 2010, 03:45:13 pm »
Wait!? DJ, you don't know ASM!?!? I've only been at it 2 weeks and I have almost completely remade Hexpipc (again, but this time it's only 500 bytes). Seriously though, I thought you (being the founder of this forum) would know ASM.
Let's see, the Admins experience in calc languages (from what I've noticed):
Builderboy: Basic - Excellent   Axe - Excellent   Asm - Beginner
DJ Omnimaga: Basic - Excellent   Axe - Medium   Asm - Beginner
Eeems: Basic - Medium   Axe - Medium   Asm - Medium
Hot_Dog: Basic - Medium   Axe - Beginner   Asm - Excellent
ztrumpet: Basic - Excellent   Axe - Excellent   Asm - Beginner

As you can see, not all of the admins know all the languages. :)