Need help with assembly language exam question

Discussion in 'Programming' started by the axiom, Jan 11, 2006.

  1. #1
    Hello

    I need help with an A level computing exam that is coming up very soon.

    Sometimes in the exam students are asked to write some assembly language code. A typical question is:

    Using the trace table below write the assembly language equivalent of the following:

    X=0
    While Not (x = 999)
    Do x = x + 1
    End While

    (In the exam the trace table contains the columns “label”, “opcode” and “operand”)

    And the answer would be:

    LDA #0
    Loop CMP #999
    BEQ Label1
    ADD #1
    JMP Loop
    Label1 STA X

    (the above code is filled in the relevant columns)

    However last year the exam asked a question that seems different from all the previous years:

    Using the trace table below write the assembly language equivalent of the following:

    Count = 0
    Repeat
    x = x + x
    count = count + 1
    until x >= 999

    you can assume that the program counter will contain the address of location “start” to execute your code.

    (then the trace table is partially filled in and the students need to fill in the rest)

    Label	Opcode	Operand	comment
    x		15	Location x contains value 15
    count			This location is reserved to store the value of count
    start
    Code (markup):
    The model answer provided by the exam board was:


    Start: LDA #0
    STA Count Store zero in Count
    Loop LDA X Load value from location X into accumulator
    ADD X Add contents of X to accumulator
    STA X Save contents of accumulator at location labelled X
    INC Count Increment value in Count
    CMP #999 Compare contents of accumulator with 999
    BLT Loop Conditional branch if contents of accumulator is < 999


    Now I understand most of the code in the answer except two things:

    1, the line “INC count” why isn’t count loaded into a register and then incremented and then after the algorithm is finished it could be stored in the memory location? Is it possible to add to a memory location without loading their value into a register? E.g. ADD memlocation1,#1

    2, if it is possible to add to memory locations without having to load their contents into a register then why is value of X loaded into the accumulator and then incremented and the stored back into the memory location for every set of the iteration? Wouldn’t it be better to write STA X just once at the end of the code?

    I would really appreciate it if someone could help me with this. I need to understand the model answer given by the exam board so that similar answers can be given for future questions.
     
    the axiom, Jan 11, 2006 IP
  2. the axiom

    the axiom Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    dont worry i worked it out
     
    the axiom, Jan 12, 2006 IP
  3. jimrthy

    jimrthy Guest

    Messages:
    283
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But what was the answer?

    That's higher level asm than I'm used to, but still...I have the same questions you did.

    (And it's been several years since I looked at asm at all, so you're ahead of me here).
     
    jimrthy, Jan 21, 2006 IP
  4. david_sakh

    david_sakh Peon

    Messages:
    1,225
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I've only worked with sparc, can't help ya.

    that looks like macro stuff.
     
    david_sakh, Jan 22, 2006 IP
  5. infonote

    infonote Well-Known Member

    Messages:
    4,032
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    160
    #5
    infonote, Jan 22, 2006 IP
  6. iamdownloader

    iamdownloader Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks infonote!

    I have similar problem with axiom question... Thanks a lot.
     
    iamdownloader, Oct 25, 2009 IP