开发者

Assembler Language Programming

开发者 https://www.devze.com 2022-12-16 05:55 出处:网络
I am trying to write a program that inputs a positive number less than 10 and outputs the sum of the first numbers. For example 5 would be 5+4+3+2+1. The commands are Stop, Load, Store, Add, Sum, Mult

I am trying to write a program that inputs a positive number less than 10 and outputs the sum of the first numbers. For example 5 would be 5+4+3+2+1. The commands are Stop, Load, Store, Add, Sum, Multiply, Divide, Input, Output, Branch, Branch if 0, and branch if not 0. Can anyone help me out here. I am kind of stuck. well what I have written is:

      IN    n
      LD    n
      ADD   sum
      STO   sum
      LD    n
      SUB   one
      ADD   sum
      STO   sum
      BRGT  haw
      LD    n
      BR    done
haw:  OUT   sum
done: STOP
      DC    n   4
      DC    sum 0
      DC    one 1  

Well the way I see it working is you load some number n and add a su开发者_StackOverflow社区m of 0 and then store this sum as n+sum. Then you load n again and subtract 1 and store that as the new sum. But I need it to repeat this until n reaches 0. So how do I do this?

Ok so what i need to do is use the branch if 0 and branch if not 0. I think I have it? so:

     IN    n
     LD    n
     ADD   sum
     STO   sum  
     BR    CAW
CAW: LD    n  
     SUB   ONE  
     STO   n
     BRGT  HAW
     BZ    TAW
HAW: ADD   SUM  
     STO   SUM  
     BR    CAW  
TAW: OUT   SUM
     DC    SUM 0
     DC    ONE 1

DC=Designated Constant, but what I need to repeat is the subtracting by one and adding the sum until n reaches 0. Branch if not zero is BRGT and branch if zero is BZ and branch is BR, LD is load. I know what I need to repeat but I don't know how you do this in assembly language.


Since this sounds like homework, I'll start with some pieces.

  1. Have you worked out the logic to this problem yet? "Kind of stuck" could mean you have no idea how this needs to be done, or that you're not sure how to implement it with the available instructions. If you haven't worked out the logic, consider this - how do you come up with the terms, how do you add them together, and what do you do with the results?

  2. If you have worked out the logic, then which commands will perform each part?

If you have more specific questions feel free to update and I'll respond, but generally the policy on SO is to offer guidance rather than just write code for (possible?) homework questions.

Edit: ok great, you've got some code, and it looks like you're on the right track but not there yet. The first thing I would ask you is whether you have tried to execute the code on paper. Pick an arbitrary input (say 5, as in your example) and step through the code one instruction at a time to see if the logic that the program performs follows the logic that you came up with in step 1. Right now I believe the program doesn't work as written; see if you can find out why and if not I'll give you a hint.

Edit 2: awesome, you're so close that you already have the answer and just need to code it. You said you want to repeat until n reaches 0. So, which command will let you take one of two branches depending on whether n is 0 or not?

Edit 3: you are correct in assuming that you need to use branch if 0 / branch not 0, but I don't see any of those in your latest code. Did I miss something? Also, what is DC for in this case? It might be helpful if you post a key so I know exactly which instructions you're using. As I said before, try executing your program by hand - that will show you where the bugs are. My hint for you right now is to identify the portion of your code that needs to be executed repeatedly and find out if it actually does get executed repeatedly.


Try writing out the problem in a high-level pseudo-code first.

Then translate it to your assembly language.

You can find help on programming in assembly language here

http://www.laynetworks.com/assembly%20tutorials.htm

This smell like a school assignment, so I think that should be enough to get you going.


think about it in C (assuming you know it)

int sumnumbers(int input)
{
    int output = 0;

    input = max(input, 10);

    switch (input)
    {
        case 5:
            output += 5;
        case 4:
            output += 4;
        case 3:
            output += 3;
        case 2:
            output += 2;
        case 1:
            output += 1;
        case 0:
            output += 0;
            break;
    }

    return input;
}

note the lack of break statements (except on the 0'th case).

Does this help?

0

精彩评论

暂无评论...
验证码 换一张
取 消