Rough Book

random musings of just another computer nerd

Tag: assembly language

Data and Code

Inspired by The Codeless Code:

A novice monk had just started learning assembly programming when he was troubled by doubt. He approached his master and asked:

“Master, how do I know which is code and which is data?”

The master who was meditating, opened his eyes, smiled, and said:

“Each is the other, yet neither is either.”

“Master, I do not understand.”, said the disciple.

The master then brought out two identical pots and said. “Take these. Fill one with the water from the lake, and fill the other with water from the stream that flows into the lake. Then bring them to me.”

The monk bowed and took the pots. He walked to the lake, which was some distance away and filled it with water from the lake. Then he walked around the side of the lake until he found the stream that fed water into the lake. He used this water to fill the second pot and then brought both pots back to his master and set them at his feet. The master looked at the pots, and then back to his disciple and said “Now go. You may come back tomorrow morning.”

The disciple came back the next morning to find his master standing with the two pots. He held up both the pots and then threw them to the opposite sides of the room. The pots smashed, and the water from both pots flowed towards the center of the room, forming a puddle.

The master then said, “Which pot contained water from the stream? Which pot contained water from the lake?” He then pointed to the growing puddle that was forming in the middle of the room. “Which part of the puddle contains water from the stream? Which part contains water from the lake?”

In that moment, the novice was enlightened.

if-else in bAdkOde

I was talking to my CSE 200 (my first CS class at ASU) professor Richard Whitehouse about bAdkOde and he (rightly) pointed out that it didn’t have an explicit selection statement. He also said that unless I wanted it to be really ugly I’d need to have a selection statement. However, since I was going for ugly I figured that I’d just emulate the operation of an if and an if-else with the existing while statement.

If you know assembly, then you know that a while is simply a set of statements wrapped with a conditional branch at the top and a backwards branch at the bottom (or in other words, an if with a goto at the end. A do-while is simply a set of statements with a conditional branch (at the bottom) that branches to the top of the loop. In fact, in assembly programming there really aren’t any for loops or while loops. These keywords are simply abstractions and syntactic sugar. In bAdkOde, you can implement an if with the existing while statement if you explicitly make it break out of the loop. For example, let’s say that we want to check whether the user entered the character “0”:

?a
-48a
# print a line break
"10
# if a is zero
{=a
 # print the string "zero"
 "122"101"114"111"10
 # set the a register to a non-zero value so that we can break out of the loop
 >1a
}

I knew there was a way to implement an if-else with just while statements but I didn’t remember exactly how. Then my friend and co-worker Juan reminded me that I needed two variables. In our case, we need to use two registers:

?a
-48a
# copy the value of a into b
>ab
"10
# if a is zero
{=a
 # print the string "zero"
 "122"101"114"111"10
 # set the a register to a non-zero value so that we can break out of the loop
 >1a
}
# if the top loop failed, it means that b (which holds the same value as a) is 
# non-zero and so we can enter this block.
# if the top loop was successful, it means that b (which holds the same value
# as a) is zero and so we won't enter this block. Hence, the second block acts
# as an 'else'
{!b
 # print the string "non-zero"
 "110"111"110"45"122"101"114"111"10
 # zero out the b register
 >0b
}

Yes, quite ugly. But that’s what I’m going for! 🙂

421 permeates the core of my being

Ah 421! I love this class. It’s so hard. It’s a lot of work, yet I love it. I love programming in Assembly. This is where you’re but one level from the processor… manipulating its guts. Telling it what to do. There are no nice if-else statements here. No print statements. It’s at a way more primitive level than that.

When you think about it, all you are maniuplating are a bunch of 1’s and 0’s. But there is an underlying logic in those numbers – that is what you need to exploit. There is no distinction between data and program code. One is the other. They are all the same. You make order out of chaos. Assembly programming is an intricate puzzle. It tests your ingenuity. Sometimes when you have an epiphany it is as if the heavens opened and the angels sang…

All original content on these pages is fingerprinted and certified by Digiprove
%d bloggers like this: