English 中文(简体)
Assembler Language Programming
原标题:

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 sum 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?





相关问题
List of suspected Malicious patterns

I am doing an anti-virus project by disassembling its code and analyzing it. So I want a list of the Suspected Malicious pattern codes, so I can observe which is suspected and which is not? So I want ...

Prefetch for Intel Core 2 Duo

Has anyone had experience using prefetch instructions for the Core 2 Duo processor? I ve been using the (standard?) prefetch set (prefetchnta, prefetcht1, etc) with success for a series of P4 ...

How are mutex and lock structures implemented?

I understand the concept of locks, mutex and other synchronization structures, but how are they implemented? Are they provided by the OS, or are these structures dependent on special CPU instructions ...

Installing GNU Assembler in OSX

No matter how hard I google, I can t seem to find a (relatively) easy-to-follow instruction on how to install the GNU Assembler on a mac. I know I can use gcc -c (Apple Clang on a Mac) to assemble .s ...

8086 assembler,INT 16,2

I got stuck at this thing,I want to see if right shift button has been pressed,so I have this assambler code: mov ah,2 int 16h ;calling INT 16,2 - Read Keyboard Flags interrupt mov ah,...

Translate a FOR to assembler

I need to translate what is commented within the method, to assembler. I have a roughly idea, but can t. Anyone can help me please? Is for an Intel x32 architecture: int secuencia ( int n, ...

热门标签