Results 1 to 5 of 5

Thread: Assembly help     submit to reddit submit to twitter

  1. #1
    Black Belt
    Join Date
    Aug 2005
    Posts
    5,907
    BG Level
    8
    FFXI Server
    Quetzalcoatl

    Assembly help

    We're currently learning Assembly, or more specifically to translate Assembly in to machine code, and I need some help as I'd really like to pass the class. But our teacher is absolutely horrible. I figured since there's a lot of different people on BG, there has to be some people who know Assembly.

    Anyway, for class we have to translate the following snippet of code from Assembly in to machine code by hand. It's for a NIOS-II card. The information we have to go by is the following:

    START_TIMER = 0xF68C
    r0 = 0x0
    r8 = 0x8
    And the code is:

    Code:
    movia r8, 50000
    subi r8, r8, 1
    bne r8, r0, START_TIMER
    The answer I've come up with is

    Code:
    00000010000000000000000001110100
    01000010001100001101010000000100
    01000010001111111111111111000100
    01000000001111011010001100011110
    since movia is a pseudo-code and gives an answer in two parts in the form of orhi and addi. Is this answer correct? Orhi states that it gives an answer in the form of rB, r0, IMMED16, %hiadj(label), but since r0 can't be written to and only returns 0 it makes no sense that it would be rB, r0 and should instead be r0, rB, or am I incorrect in that reasoning?

  2. #2
    Yoshi P
    Join Date
    Aug 2006
    Posts
    5,139
    BG Level
    8
    FFXIV Character
    Dead Gye
    FFXIV Server
    Lamia
    FFXI Server
    Ragnarok

    First thing to note is that assembly is specific to the architecture you're working on. MIPS assembly is different from Atmel assembly is different from what you're using, etc.

    Every form of assembly should be well documented, including how the assembly is formatted into machine code. I googled "NIOS II assembly" and this datasheet was the first thing I clicked on. It seems to be what you should be using. http://www.altera.com/literature/hb/...u_nii51017.pdf

    movia is indeed a pseudo instruction. However since it stands for two instructions you should have a grand total of 4 lines of machine code. subI is also a pseudo instruction.

    Code:
    movia r8, 50000
    subi r8, r8, 1
    bne r8, r0, START_TIMER
    This can be re-written as:

    Code:
    orhi r8, r0, %hiadj(50000)
    addi, r8, r0, %lo(50000)
    addi r8, r8, (-1)
    bne r8, r0, START_TIMER
    The next important thing you need to figure out is what type each instruction is. Is it an R-type, an I-type, or a J-type. The datasheet will usually tell you this and more. In fact the one linked gives you the proper layout for every instruction.

    orhi rB, rA, IMM16 = [A][B][IMM16][0x34]
    addi rB, rA, IMM16 = [A][B][IMM16][0x04]
    addi rB, rA, IMM16 = [A][B][IMM16][0x04]
    bne rA, rB, label = [A][B][label][0x1e]

    So with the values you were given you plug them in and end up with...

    Code:
    [0x00][0x08][0x0001][0x34]
    [0x00][0x08][0xC350][0x04]
    [0x08][0x08][0xFFFF][0x04]
    [0x08][0x00][0xF68C][0x1e]
    Which when turned into machine code, matching the proper amount of bits should be..

    Code:
    00000 01000 0000000000000001 110100
    00000 01000 1100001101010000 000100
    01000 01000 1111111111111111 000100
    01000 00000 1111011010001100 011110
    Your answers seem to match mine so I believe you did everything correctly. As for your question about your reasoning at the end. Typically in assembly the first register is the register that you'll be writing too. That's why it's in the form of "blah rB, rA, IMM16" even though the machine code is "[A][B][IMM16][opcode]". So you aren't actually writing to r0 in any of those instructions. (The bne instruction is a compare so it doesn't write to B).

    You also correctly handled the %hiadj macro. I probably would have mistaken it for the %hi macro if I didn't notice the macro explanations in the datasheet.

    -------------------------

    Unless you start working with architecture design or stuff like that, transforming assembly into machine code can be a very simple process. If you're required to memorize layouts, only memorize the layout for the generic types: R-type (Register), I-type (Immediate), and J-type (Jump). Then you just have to use context/knowledge to figure out which type each instruction is and move the arguments into the correct order. The only tricky parts would be dealing with pseudo instructions.

    If you have any other questions, or if I didn't properly explain something (4:00AM here and all) let me know.

  3. #3
    Black Belt
    Join Date
    Aug 2005
    Posts
    5,907
    BG Level
    8
    FFXI Server
    Quetzalcoatl

    Thank you, you did answer it

    The course is very fast paced, and we've only been learning Assembly for a few days before we were told to start translating in to machine code, so I wanted to be sure I was doing it the correct way. The teacher is really horrible at explaining things, and has a very hard to understand accent :/

  4. #4
    Ridill
    Join Date
    May 2005
    Posts
    13,568
    BG Level
    9

    I don't know why you'd want to take more than a few days to go from "here's assembly" to "translate into binary". Assembly -> binary is essentially just a bijection. If your class took like 3 weeks to get there, I'd be worried that you weren't going to learn anything useful in the space of a semester!

    Anyway, Deadgye nailed this like TA-style good.

  5. #5
    Black Belt
    Join Date
    Aug 2005
    Posts
    5,907
    BG Level
    8
    FFXI Server
    Quetzalcoatl

    I suppose. The thing us, he just gave us a 400 page handbook and told us to figure it out, he hasn't demonstrated how to do it, which is why I asked if I was doing it right. Anyway, I got the answer I was looking for, so thank you

Similar Threads

  1. WAR/NIN help plz
    By cyphx in forum General Discussion
    Replies: 18
    Last Post: 2006-04-28, 14:11
  2. Ouch, Call for help on Cassie{/comfort}
    By Deadkennedys in forum General Discussion
    Replies: 3
    Last Post: 2004-09-27, 07:48
  3. Need help on money making methods
    By in forum General Discussion
    Replies: 2
    Last Post: 2004-09-16, 21:57