Computer Science: Assembly Language Program

In summary: Count jmp Loop N1: ... ; Set N1 to the value in Ans inc N1 lod Ans ; Store the value in Ans in N1 inc N1 lod Count ; Increment Count by 1
  • #1
intellect
9
0
Write an assembly language program that uses ten memory locations (variables) filled with 0, starting at Address 30, and that stores the values 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, and 22 in them. Your program will store 2 at Address 30, 4 at address 31, 6 at address 32, etc. Your program must use a loop. [/B]


I have my attempted solution below. The two problems I'm having are that 1) the numbers are incrementing by one instead of two (location 30-2, location 31-3, etc.), and 2) I can't get the program to stop after the number 22 is displayed in location 40. Could you please help me? Thank you.

lod-c 2 ; Start with a 2 in location 12
sto 12

lod 12 ; This instruction is stored in location 2
inc
sto 13 ; This instruction is stored in location 4


lod 2 ; Add 1 to the number in location 2
inc
sto 2


lod 4 ; Add 1 to the number in location 4
inc
sto 4

jmz done
jmp 2 ; Go back to the instruction in location 2


done: hlt
 
Physics news on Phys.org
  • #2
It would help if I knew what assembly language this is.

A couple of quick comments.

To use a loop, you'll need a loop index. From the loop index, you can figure out where the current number should be stored. You can also figure out what number to store there from the index.

The jump for the loop probably needs a label to jump back to until everything is done. I doubt you need to try to jump to a specific location by number.

Once we know the instruction set and registers available, we should be able to help further.
 
  • #3
Hi Grep. I'm still trying to figure out the loop index for this program. I'm using base ten assembly language for the xComputer, and my computer is a Windows. I hope that I'll be able to figure something out so that I can consult with you on it. Thank you for your advice.
 
  • #4
This looks like what you're talking about - http://math.hws.edu/eck/cs120/f02/lab3/

I don't know if this is the class you're taking, but it has some good information and examples. There are a couple of examples in the Labels (and More Programs) section that you should look at. You will need a variable or two (that's where the labels come in) to solve this problem.
 
  • #5
Thank you so much for the website, Mark44! I read through the material, but unfortunately, I still seem to be struggling with the problem. I tried to insert labels to see if that would work, but up to this point, the program still isn't working. I will continue working on it as long as I need to.
 
  • #7
I have the program below. The idea is to use a loop in order to list the even numbers 2-22 ( 2 and 22 included) in memory locations 30-40, and then stop. The program I have below only goes through 16, it doesn't seem to stop, and I don't know if the loop is quite right (there's no actual loop index, I had trouble with this part).

lod-c 2 ; Start with a 2 in location 30
sto 30

lod 30
inc
inc
sto 31


lod 31
inc
inc
sto 32

lod 32
inc
inc
sto 33

lod 33
inc
inc
sto 34

lod 34
inc
inc
sto 35

lod 35
inc
inc
sto 36

lod 36
inc
inc
sto 37

lod 37
inc
inc
sto 38

lod 38
inc
inc
sto 39

lod 39
inc
inc
sto 40


jmz done
jmp 2 ; Go back to the instruction in location 2


done: hlt
 
  • #8
There's no loop index, and there's no loop!

Here's one of the examples on the page that I provided the link for. I have modified this example slightly by adding variables at the end of the program (Count, N1, N2, and Ans). It is supposed to multiply 5 * 4 by adding 5 to itself 4 times (i.e., 5 + 5 + 5 + 5) to get 20.

Put this code in your emulator and single step through it to watch what happens. I can't guarantee that it works as advertised, because I haven't tried it out.

This should give you an idea of what is meant by writing code that uses a loop.
Code:
Loop:   Lod Count   ; If Count is equal to N2, then N1 has
          Sub N2      ;    been added to itself N2 times, and
          Jmz Done    ;    the program is done.

          Lod Count   ; Add 1 to Count
          Inc
          Sto Count

          Lod Ans     ; Ans contains N1 + N1 + ...;  Add in 
          Add N1      ;   another copy of N1 to Ans
          Sto Ans

          Jmp Loop    ; Return to start of loop

Done:   Hlt
Count: 0
N1: 5
N2: 4
Ans: 0

BTW, some of the code on the Web page has comments that don't match what is going on.
Code:
    LOD 4    ; Copy the number in location 3 into the AC
    ADD 5    ; Add the number from location 4 to the AC
    STO 10   ; Store the answer in memory location 10
    HLT      ; Stop the computer
    209      ; This is location 4, so this is one of the numbers.
    52       ; This is location 5, so this is the other number.
It looks to me like the first statement is copying the number in location 4, not 3. And the next statement is adding the number from location 5, not 4.
 
  • #9
I took your advice, and I was able to come up with more of a loop format. I don't know what it is about this problem, but I can't get the right numbers in the right increments. My new program is a loop starting at location 30 but 1) the program starts with a 2 in location 30, then changes it to 31, then the list continues 32, 33, 34, etc. instead of the list being 2, 4, 6...22, which means that the numbers are also incrememting by 1, instead of just listing the evens. I'd appreciate it if you could look at my new program (I'll list below). I only have about an hour and a half left before this assignment is due, though, but I'd appreciate any other advice that you could give me. Thank you so much for all of your help. My new program is below:

start:lod-c 2

loop:sto-i index
add-c 2
lod index
inc
sto index

jmp loop



lod counter
sub-c 1
sto counter


jmz done
jmp loop

done:hlt
@28
counter:11
index:30
 
  • #10
intellect said:
I took your advice, and I was able to come up with more of a loop format. I don't know what it is about this problem, but I can't get the right numbers in the right increments. My new program is a loop starting at location 30 but 1) the program starts with a 2 in location 30, then changes it to 31, then the list continues 32, 33, 34, etc. instead of the list being 2, 4, 6...22, which means that the numbers are also incrememting by 1, instead of just listing the evens. I'd appreciate it if you could look at my new program (I'll list below). I only have about an hour and a half left before this assignment is due, though, but I'd appreciate any other advice that you could give me. Thank you so much for all of your help. My new program is below:

start:lod-c 2

loop:sto-i index
add-c 2
lod index
inc
sto index

jmp loop



lod counter
sub-c 1
sto counter


jmz done
jmp loop

done:hlt
@28
counter:11
index:30

That's a much better approximation of what this code should look like than what you started with. To get you a little close, I would use another variable, say value, with a starting value of 0. I would also start with an index value of 29.

Each iteration of your loop do these things:
1) Increment value by 2 (load it into the accumulator, then two inc instructions)
2) Increment index by 1 (load it into the accumulator, then inc)
3) Load value into the accumulator
4) Store what's in the accumulator to location index.
5) Decrement counter (load counter into the accumulator, then dec)
6) If counter == 0 you're done. Otherwise jump to the start of your loop (step 1 here) for another iteration.

See if you understand what I'm doing here by pretending to be the computer for a couple of iterations of this loop. Do this with a piece of paper and a pencil or pen. You're starting off with value == 0, index == 29, and counter == 11. Go through the steps above two complete iterations. After you have done step 6 for the second time, you should end up with 2 stored in loc 30, 4 stored in loc 31, and counter set to 9, value set to 6, and index set to 32.
 
  • #11
Wow! That's makes so much more sense! Based off what you said, I have a program that gives me the right numbers and that stops at the right time. There's just one more thing: All of the even numbers 2-22 show up in location 30 instead of being a list (I can't understand why). I just found out that I have more time to work on this, so if you could help me with this last part, I would truly be grateful. You have been a livesaver! Thank you sooo much! Here is my program:

loop:lod value; load current value and put in accumulator
inc
inc
sto value; store value back


lod index; increment index
add-c 1
sto index


lod counter
dec
sto counter

jmz done
jmp loop

done:hlt
@28
counter:11
index:29
value:0
 
  • #12
I put your code inside [ code] and [ /code] tags (but don't add the spaces that I did. It's good practice on this and other forums to use these HTML tags.
intellect said:
Wow! That's makes so much more sense! Based off what you said, I have a program that gives me the right numbers and that stops at the right time. There's just one more thing: All of the even numbers 2-22 show up in location 30 instead of being a list (I can't understand why). I just found out that I have more time to work on this, so if you could help me with this last part, I would truly be grateful. You have been a livesaver! Thank you sooo much! Here is my program:
Code:
loop:lod value; load current value and put in accumulator
     inc
     inc
     sto value; store value back
      

     lod index; increment index
     add-c 1
     sto index
    
      
     lod counter
     dec
     sto counter

     jmz done
     jmp loop

done:hlt
@28
counter:11
index:29 
value:0
In your code where you're incrementing index, all you are doing is incrementing it, but you aren't using index as the location at which to store value. That's how you get the different numbers to go to different places. Basically you're going to store 2 at location index, store 4 at location index + 1, store 6 at location index + 2, etc.

Here is the algorithm I presented earlier, with a couple of changes to make it clearer.
Each iteration of your loop do these things:
1) Increment value by 2 (load it into the accumulator, then two inc instructions, then store it at location value).
2) Increment index by 1 (load it into the accumulator, then inc, then store it at location index).
3) Load value into the accumulator
4) Store what's in the accumulator to location index.
5) Decrement counter (load counter into the accumulator, then dec, the store it at location counter).
6) If counter == 0 you're done. Otherwise jump to the start of your loop (step 1 here) for another iteration.


In your code where you add 1 to the accumulator I would instead do this:
Code:
lod index
inc
sto index
If all you need to do is add 1 or subtract 1, use inc or dec. For the other place where I advised using inc and then inc again, that would be a reasonable place to use add-c 2. Either one is fine, though.
 
  • #13
Thank you so much! It finally works! Yes! I wouldn't have been able to understand this problem had it not been for you, Mark44. You are amazing! Thanks again!
 

1. What is assembly language?

Assembly language is a low-level programming language that is used to write instructions for a specific computer architecture. It is considered a symbolic representation of machine code, which is the binary language that computers understand.

2. How is assembly language different from high-level languages?

Assembly language is much closer to the machine code that computers understand, whereas high-level languages are more human-friendly and abstracted from the hardware. This means that assembly language is more difficult to read and write, but it allows for more precise control over the computer's operations.

3. What are the advantages of using assembly language?

Assembly language is highly efficient and allows for direct access to the computer's hardware and memory. This makes it ideal for tasks that require a high level of speed and control, such as operating system development, game programming, and device drivers.

4. Are there any disadvantages to using assembly language?

One major disadvantage of assembly language is that it is machine-specific, meaning that code written for one type of computer may not work on another. Additionally, it is more difficult to learn and debug compared to high-level languages.

5. Is assembly language still relevant in modern computer science?

While high-level languages have become more prevalent in modern computer science, assembly language still has its uses. It is often used in embedded systems, real-time applications, and low-level programming tasks. Additionally, understanding assembly language can provide a deeper understanding of how computers work at a fundamental level.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
17K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top