Computer science machine language help! If-then-otherwise statements
February 7, 2015 6:39 PM Subscribe
We are working on simple machine language in computer science for data manipulation. All the other problems I have gotten through but this last one. Here is the problem:
"Write a short program in machine language to perform requested activity. Assume the program is placed in memory starting at address 00-
-If the value stored in memory location 44 is 00, then place the value 01 in memory location 46; otherwise, put the value FF in memory location 46."
Lots of questions inside DX Working with very basic Op-code and Operand setups.
Alright, I've been cracking my skull against the table repeatedly trying to figure this problem out. I don't know I am having such issues with it anymore, at this point I wonder if I am missing something so simple.
We are working on simple machine language in computer science for data manipulation, working with very basic Op-code and Operand setups. All the other problems I have gotten through but this last one. Here is the problem:
"Write a short program in machine language to perform requested activity. Assume the program is placed in memory starting at address 00-
-If the value stored in memory location 44 is 00, then place the value 01 in memory location 46; otherwise, put the value FF in memory location 46."
So I think the first thing that trips me up is that the problem says "memory location" instead of memory address or cell. At what point did these terms become interchangeable and why doesn't my text say they are in the first place? A little frustrating to be essentially learning a new language and they switch words on yah DX
But moving along, my other problem is whether or not I have to actually apply values into registers to begin with and to what extent. For example, I know I will need to use JUMP conditions in, but will I need to create an ADD loop in also? Or should I just be making values 00 and such into other registers to reference back to when adding or taking out values into memory location 44? It's just that the JUMP instruction always references back to register 0 and what value is in it, correct? So then I should have other registers with the values 00 and 01 to be able to shuffle in and out of register 0 in order to create the if-then-otherwise loop?
So many questions! Any help at all is immensely appreciated! Any attempts I have tried with google gives me too in-depth of other machine languages. If you guys need more details, feel free to ask anything at all ^_^
Alright, I've been cracking my skull against the table repeatedly trying to figure this problem out. I don't know I am having such issues with it anymore, at this point I wonder if I am missing something so simple.
We are working on simple machine language in computer science for data manipulation, working with very basic Op-code and Operand setups. All the other problems I have gotten through but this last one. Here is the problem:
"Write a short program in machine language to perform requested activity. Assume the program is placed in memory starting at address 00-
-If the value stored in memory location 44 is 00, then place the value 01 in memory location 46; otherwise, put the value FF in memory location 46."
So I think the first thing that trips me up is that the problem says "memory location" instead of memory address or cell. At what point did these terms become interchangeable and why doesn't my text say they are in the first place? A little frustrating to be essentially learning a new language and they switch words on yah DX
But moving along, my other problem is whether or not I have to actually apply values into registers to begin with and to what extent. For example, I know I will need to use JUMP conditions in, but will I need to create an ADD loop in also? Or should I just be making values 00 and such into other registers to reference back to when adding or taking out values into memory location 44? It's just that the JUMP instruction always references back to register 0 and what value is in it, correct? So then I should have other registers with the values 00 and 01 to be able to shuffle in and out of register 0 in order to create the if-then-otherwise loop?
So many questions! Any help at all is immensely appreciated! Any attempts I have tried with google gives me too in-depth of other machine languages. If you guys need more details, feel free to ask anything at all ^_^
Best answer: It is pretty hard for me to understand what you're asking, but hopefully this will help somewhat:
Yes, a memory location is the same thing as a memory address.
You will not have any loops at all. A loop is when you do something over and over. You only have to do anything once. You shouldn't have to add anything either.
The general structure of your program will look something like this:
posted by dfan at 7:11 PM on February 7, 2015 [1 favorite]
Yes, a memory location is the same thing as a memory address.
You will not have any loops at all. A loop is when you do something over and over. You only have to do anything once. You shouldn't have to add anything either.
The general structure of your program will look something like this:
The actual instructions will depend on your processor.0: load the contents of address 44 into register 0 1: compare register 0 to 00 2: jump to 5 if true 3: load the value ff into address 46 4: jump to 6 5: load the value 01 into address 46 6: nop
posted by dfan at 7:11 PM on February 7, 2015 [1 favorite]
Response by poster: Teraflop:
That is part of my issue, I'm not even sure what kind of machine language we are working with. The class I am in is called Computer Science Concepts and is the first course we take before doing anything else in computer science, so we are at the very beginning of it all. Not to mention it is all online and so communication with the teacher is terrible, to say the least.
Here is some of the condensed info from the textbook, hopefully it will help give you an idea of what it is they mean:
"The machine we will use for our discussion has 16 general-purpose registers and 256 main memory cells, each with a capacity of 8 bits. Labels and addresses are in values of base 2 and bit patterns are in hexadecimal. Thus, registers are labeled 0 through F, memory cells addressed 00 through FF. Instruction consists of two parts: op-code and operand. There are only 12 basic instructions (some are load, store, move, add, and, or, exclusive, rotate, and jump)."
I guess part of my problem is also with the jump instruction. It states that in the description about how the address in the jump instruction is what gets copied into the program counter during the execute phase if the register listed and register 0 are the same, thus causing the next instruction to have to be whatever that address was. I don't know, maybe I am just thinking of the JUMP instruction in the wrong way :(
Dfan:
Thank you for that general structure! I am a visual person and that helps a lot. But then would the 0: actually be 00/01 since the counter takes from two cells at a time to create the entire instruction?
Overall, thank you both so far for your help! Even if you both don't quite understand what exactly I am trying to say, it is your questions that help clarify the situation and also help me understand this in better detail.
posted by LittleNami at 7:29 PM on February 7, 2015
That is part of my issue, I'm not even sure what kind of machine language we are working with. The class I am in is called Computer Science Concepts and is the first course we take before doing anything else in computer science, so we are at the very beginning of it all. Not to mention it is all online and so communication with the teacher is terrible, to say the least.
Here is some of the condensed info from the textbook, hopefully it will help give you an idea of what it is they mean:
"The machine we will use for our discussion has 16 general-purpose registers and 256 main memory cells, each with a capacity of 8 bits. Labels and addresses are in values of base 2 and bit patterns are in hexadecimal. Thus, registers are labeled 0 through F, memory cells addressed 00 through FF. Instruction consists of two parts: op-code and operand. There are only 12 basic instructions (some are load, store, move, add, and, or, exclusive, rotate, and jump)."
I guess part of my problem is also with the jump instruction. It states that in the description about how the address in the jump instruction is what gets copied into the program counter during the execute phase if the register listed and register 0 are the same, thus causing the next instruction to have to be whatever that address was. I don't know, maybe I am just thinking of the JUMP instruction in the wrong way :(
Dfan:
Thank you for that general structure! I am a visual person and that helps a lot. But then would the 0: actually be 00/01 since the counter takes from two cells at a time to create the entire instruction?
Overall, thank you both so far for your help! Even if you both don't quite understand what exactly I am trying to say, it is your questions that help clarify the situation and also help me understand this in better detail.
posted by LittleNami at 7:29 PM on February 7, 2015
Best answer: It sounds like you're working in a made-up machine language of the sort often used for intro courses. It also sounds like there are some fairly serious issues with your course.
That said, can you paste the full description of the JUMP instruction? From what you've said, it sounds like the JUMP instruction in this assignment is a conditional one that takes two arguments: an address, A, and a register, which we'll call R. When the JUMP instruction is executed, the value in register 0 is compared to the value in register R. If they are equal, then the next instruction executed will be whatever is at address A. If they are not equal, the next instruction executed will just be the next one in the sequence (like the JUMP never happened).
So, and this is just based on what you've told us here, JUMP works something like this (I don't know if your instructions should really be 00/01 or not, as that depends on information about the processor you haven't given us here):
posted by zachlipton at 8:10 PM on February 7, 2015 [1 favorite]
That said, can you paste the full description of the JUMP instruction? From what you've said, it sounds like the JUMP instruction in this assignment is a conditional one that takes two arguments: an address, A, and a register, which we'll call R. When the JUMP instruction is executed, the value in register 0 is compared to the value in register R. If they are equal, then the next instruction executed will be whatever is at address A. If they are not equal, the next instruction executed will just be the next one in the sequence (like the JUMP never happened).
So, and this is just based on what you've told us here, JUMP works something like this (I don't know if your instructions should really be 00/01 or not, as that depends on information about the processor you haven't given us here):
0: load the contents of address 44 into register 0 1: load value 00 into register 1 2: JUMP R1 6 // what does this do? it compares R0 and R1. // If they are equal, it jumps control to the instruction at 6. If they are not equal, control continues to the instruction at 3. 3: store the value FF in address 46 // if we're here, R0 and R1 are not equal 4: JUMP R0 7 5: store the value 01 in address 46 // if we're here, R0 and R1 are equal 6: nopThe only thing a little strange in there is what I had to do at lines 4. Ideally, line 4 would just jump directly to instruction #7, as we've already set address 46 and have completed the assigned task. However, at least with the information you've given us here, we don't have a basic jump instruction that always jumps to a specified address. So what do we do? We compare R0 to itself. Since the value in R0 always equals the value in R0, this instruction will always jump to line 6. Line 6 is simply a nop, as we need someplace to jump to and be the end of the program.
posted by zachlipton at 8:10 PM on February 7, 2015 [1 favorite]
Response by poster: Zachlipton:
About the processor bit, there was nothing mentioned about processors in our book. Nothing is actually being done on a computer yet, it's all theory, which makes me lean to agree with you in the made-up language part.
Here is the exact definition on our JUMP instruction:
"B RXY"
"JUMP to the instruction located in memory cell at address XY if bit pattern in register R is equal to bit pattern in register 0. Otherwise, continue with normal sequence of execution. (The jump is implemented by copying XY into the program counter during execution phase.)
Example: B43C would first compare the contents of register 4 with contents of register 0. It the two are equal, the pattern 3C would be placed in the program counter so that the next instruction executed would be the one located at that memory address. Otherwise, nothing would be done and program execution would continue in its normal sequence."
I hope that helps! Your example also was great for a visual!
posted by LittleNami at 8:30 PM on February 7, 2015
About the processor bit, there was nothing mentioned about processors in our book. Nothing is actually being done on a computer yet, it's all theory, which makes me lean to agree with you in the made-up language part.
Here is the exact definition on our JUMP instruction:
"B RXY"
"JUMP to the instruction located in memory cell at address XY if bit pattern in register R is equal to bit pattern in register 0. Otherwise, continue with normal sequence of execution. (The jump is implemented by copying XY into the program counter during execution phase.)
Example: B43C would first compare the contents of register 4 with contents of register 0. It the two are equal, the pattern 3C would be placed in the program counter so that the next instruction executed would be the one located at that memory address. Otherwise, nothing would be done and program execution would continue in its normal sequence."
I hope that helps! Your example also was great for a visual!
posted by LittleNami at 8:30 PM on February 7, 2015
Best answer: Ok. So in the rough example I gave, when I said "JUMP R1 6":
R=1
XY=06
Do you feel you understand how the JUMP instruction works now?
posted by zachlipton at 9:23 PM on February 7, 2015 [1 favorite]
R=1
XY=06
Do you feel you understand how the JUMP instruction works now?
posted by zachlipton at 9:23 PM on February 7, 2015 [1 favorite]
Response by poster: YEEEEEEEEEEESSSS!! Thank you guys so much! A little overly enthusiastic, I know, but I've been effing frustrated with this for so long. Thank you thank you thank you!
posted by LittleNami at 9:33 PM on February 7, 2015
posted by LittleNami at 9:33 PM on February 7, 2015
2nding the notion advanced by zachlipton that there may be "some fairly serious issues with your course." Does the problem statement really say "Write a short program in machine language to perform requested activity"? This (plus some of the other example text you quote) seems very poorly written, as if the author is attempting to emulate rigor by writing stilted prose.
posted by doctor tough love at 10:03 PM on February 7, 2015 [1 favorite]
posted by doctor tough love at 10:03 PM on February 7, 2015 [1 favorite]
Just to clear up some terminology, I think the instruction "B" would normally be referred to as a "branch" instruction, which is a conditional branch in execution. "Jump" usually means an unconditional jump. (Hopefully my assembly isn't so out of date that this is no longer true...)
posted by jjwiseman at 12:20 AM on February 8, 2015
posted by jjwiseman at 12:20 AM on February 8, 2015
The CPU in question sounds like a simplified version of a classic 8-bit processor. B is a branch instruction - often you'd branch on whether a particular flag bit was 1 or 0, but branching on a register comparison works just as well.
Honestly, building up a little abstract machine like this is going to be a core part of any computer science curriculum: It doesn't sound like there's anything particularly wrong with the course to me. Dropping you straight into x86 (or any other modern CPU) assembler would be insane - you'd be swamped by concepts you haven't been exposed to yet & the learning curve would be a cliff face. Giving you an ultra-simplified CPU & instruction set to work with lets them add these concepts sequentially so you can master them one at a time.
posted by pharm at 12:48 AM on February 8, 2015 [2 favorites]
Honestly, building up a little abstract machine like this is going to be a core part of any computer science curriculum: It doesn't sound like there's anything particularly wrong with the course to me. Dropping you straight into x86 (or any other modern CPU) assembler would be insane - you'd be swamped by concepts you haven't been exposed to yet & the learning curve would be a cliff face. Giving you an ultra-simplified CPU & instruction set to work with lets them add these concepts sequentially so you can master them one at a time.
posted by pharm at 12:48 AM on February 8, 2015 [2 favorites]
There are simple little real-world 8-bit processors that would work just fine. 6502 or 6809 are prime examples.
I think the nuance here is that the teacher is trying to introduce a generic branch instruction that performs one type of branch in one operation ("compare R0 to number and branch if equal") compared to how a typical old 8-bit processor would do it. ("Compare register to {constant or address}. Branch if {equal,less,greater,etc}"). Branching depends on the carry flag, which is a whole different lesson.
posted by JoeZydeco at 12:26 PM on February 8, 2015
I think the nuance here is that the teacher is trying to introduce a generic branch instruction that performs one type of branch in one operation ("compare R0 to number and branch if equal") compared to how a typical old 8-bit processor would do it. ("Compare register to {constant or address}. Branch if {equal,less,greater,etc}"). Branching depends on the carry flag, which is a whole different lesson.
posted by JoeZydeco at 12:26 PM on February 8, 2015
Agreed on the hardware: I learned on a Z80 system. At the very least, I'd hope they have some tiny emulator, otherwise whether or not a program works correctly - or "perform requested activity" correctly - sometimes becomes a matter of opinion.
What concerns me about this class is that (based on the examples given) the textbook does not seem well-written. Ie, one of the basic instructions is "exclusive". I've seen Exclusive-or and I've seen XOR but I've never ever seen it called just "exclusive".
posted by doctor tough love at 11:13 PM on February 8, 2015
What concerns me about this class is that (based on the examples given) the textbook does not seem well-written. Ie, one of the basic instructions is "exclusive". I've seen Exclusive-or and I've seen XOR but I've never ever seen it called just "exclusive".
posted by doctor tough love at 11:13 PM on February 8, 2015
Response by poster: So I got my graded homework back and I got it correct! I will post it here in case other people need an example to go by. Zachlipton, your layout worked the best for my course.
00/01: 1044 - load the contents of address 44 into register 0
02/03: 2100 - load bit pattern 00 into register 1
04/05: 2201 - load bit pattern 01 in register 2
06/07: 23FF - load bit pattern FF in register 3
08/09: B10E – JUMP condition, this compares register 1 and register 0. If same, then jump to instruction 0E. If not, proceed normally
0A/0B : 3346 - load bit pattern FF from register 3 in address 46
0C/0D: B010 JUMP condition comparing register 0 with itself, always true to make sure the jump occurs to address 10 to halt the program rather than add the other value to address 46
0E/0F: 3246 - load bit pattern 01 from register 2 in address 46
10/11: C000 – halt
posted by LittleNami at 6:12 PM on February 13, 2015 [1 favorite]
00/01: 1044 - load the contents of address 44 into register 0
02/03: 2100 - load bit pattern 00 into register 1
04/05: 2201 - load bit pattern 01 in register 2
06/07: 23FF - load bit pattern FF in register 3
08/09: B10E – JUMP condition, this compares register 1 and register 0. If same, then jump to instruction 0E. If not, proceed normally
0A/0B : 3346 - load bit pattern FF from register 3 in address 46
0C/0D: B010 JUMP condition comparing register 0 with itself, always true to make sure the jump occurs to address 10 to halt the program rather than add the other value to address 46
0E/0F: 3246 - load bit pattern 01 from register 2 in address 46
10/11: C000 – halt
posted by LittleNami at 6:12 PM on February 13, 2015 [1 favorite]
This thread is closed to new comments.
Couple of other comments:
I don't know how what kind of answer you want about "location" versus "cell". It seems like you figured out what was meant, and none of us are likely to be able to answer why your textbook author worded things in a particular way. For what it's worth, "address" is what I think most programmers would say if they were trying to be precise.
For example, I know I will need to use JUMP conditions in, but will I need to create an ADD loop in also?
Nothing in the problem statement tells you to add anything to anything else, and nothing tells you to repeat anything so it doesn't seem like there's any need for a loop.
posted by teraflop at 7:10 PM on February 7, 2015 [2 favorites]