Thread: ASSEMBLY LEVEL Programming

  1. #16
    Registered User
    Join Date
    May 2012
    Posts
    210
    So I have been staring at this code for the last half and hour and I can say I completely understand it. I changed the format of sw function because it is written in a different way. However the program is not recognizing LCO for some reason. I am getting an error
    Code:
     unrecognized operand specifier
    Also should the print function in the end still be calling to r31? It can't call to r14 because the original value of r14 gets restored after a few steps

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    210
    Modified code thanks to you!
    Code:
    .global _open                .global _close
                    .global _read
                    .global _write
                    .global _printf
                    .global _a
    
    
                    .align 4
    _a:
                    .word 13
    
    
    LC0:
                    .ascii "output is %d\12\0"
                    .align 4
                    .global _main
    _main:
    ;;; ; ; Initialize Stack Pointer
                    add r14,r0,r0
                    lhi r14, ((memSize-4)>>16)&0xffff
                    addui r14, r14, ((memSize-4)&0xffff)
    ;;; ; ; Save the old frame pointer
                    sw -4(r14),r30
    ;;; ; ; Save the return address
                    sw -8(r14),r31
    ;;; ; ; Establish new frame pointer
                    add r30,r0,r14
    ;;; ; ; Adjust Stack Pointer
                    add r14,r14,#-24
    ;;; ; ; Save Registers
                    sw 0(r14),r3
                    sw 4(r14),r4
                    sw 8(r14),r5
                    lhi r3,(_a>>16)&0xffff
                    addui r3,r3,(_a&0xffff)
                    subi r5,r0,#1
                    lw r4,0(r3)
                    beqz r4, _l2
    _l1:    
                    addi r5,r5,#1
                    srli r4, r4,#1
                    bnez r4,_l1
    _l2:
                            
                    subi    r14,r14,#8
                    lhi     r3,(LCO>>16)&0xffff 
                    addui   r3,r3,(LCO&0xffff)
                    sw      0(r14),r3 
                    sw      4(r14),r5 
                    jal     _printf   
                    addi    r14,r14,#8 
            
    
    
    L3:
    ;;; ; ; Restore the saved registers
                    lw r3,-24(r30)
                    nop
                    lw r4,-20(r30)
                    nop
                    lw r5,-16(r30)
                    nop
    ;;; ; ; Restore return address
                    lw r31,-8(r30)
                    nop
    ;;; ; ; Restore stack pointer
                    add r14,r0,r30
    ;;; ; ; Restore frame pointer
                    lw r30,-4(r30)
    ;;; ; ;  HALT
                                    jal _exit
                                    nop
    
    
    _exit:
                                    trap #0
                                    jr r31
                                    nop
    _open:
                                    trap #1
                                    jr r31
                                    nop
    _close:
                                    trap #2
                                    jr r31
                                    nop
    _read:
                                    trap #3
                                    jr r31
                                    nop
    _write:
                                    trap #4
                                    jr r31
                                    nop
    _printf:
                                    trap #5
                                    jr r31
                                    nop

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    210
    Is there a reason that 4 isn't printing
    r5 is incremented 4 times and when we
    Code:
     sw 4(r14), r5
    and call printf after it should print

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by kiwi101 View Post
    Is there a reason that 4 isn't printing ... r5 is incremented 4 times
    r5 is initialized to -1 since log(0) = -1, log(1) = 0, log(2) = 1, log(4) = 2, log(8) = 3. If r5 is incremented 4 times, it should be 3.

    Is the _printf not working? Do you even see "the output is ..." ?

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    210
    No I can't see anything with the above code I posted there is an error:
    Code:
     pr.s(45): unrecognizable operand specifier:             lhi     r3, => (LCO>>16)&0xffff pr.s(46): unrecognizable operand specifier:             addui   r3,r3, => (LCO&0xffff)

  6. #21
    Registered User
    Join Date
    May 2012
    Posts
    210
    I don't know why it can not recognize LCO

  7. #22
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by kiwi101 View Post
    I don't know why it can not recognize LCO
    Isn't that label LC0 (L C zero)?

    Once you get done with the DLX, you should switch to learning X86 (Intel) assembly code, assuming you're running on a PC. There are more instructions to learn, but there are fewer limitations that you have to work around, such as the DLX limitation of 16 bit immediate values. The other issue for X86 assembly is the actual names of the library functions as called from assembly, but in this case, you can write a small c program that calls the functions you want to use, then have the compiler produce assembly code and use that as a reference.

  8. #23
    Registered User
    Join Date
    May 2012
    Posts
    210
    AHH I'm such a dummy

    But now this is what is printing
    Code:
    TRAP #0 received

  9. #24
    Registered User
    Join Date
    May 2012
    Posts
    210
    Even if i change the printf r31 in the end to printf r5
    some other error appears
    I'm so close this is sooo frustrating!!!

  10. #25
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    trap #0 is normal program exit. I don't know why the printf did not display anything. Is there a console window open while the program is running? Can you run the program from a console window so that you can see program output?

    printf r31
    r31 contains the return address, you should not change that to r5.

  11. #26
    Registered User
    Join Date
    May 2012
    Posts
    210
    I actually have no idea what a console window is
    So I'm assuming no its not open.
    Should I download a console window to run it on

  12. #27
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Is there some way to ask for user input like scanf or gets, or to pause the screen until the user presses some key? If so, try that to see if a console window appears. It could be that the program opens a console window automatically, but then closes it when the program completes (trap #0), and it's not on long enough to be visible.

    Are there any instructions that came with the DLX tool set that you have?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entry Level Programming Jobs
    By bengreenwood in forum General Discussions
    Replies: 5
    Last Post: 09-21-2009, 12:14 PM
  2. API Programming in network level
    By rchiu5hk in forum C++ Programming
    Replies: 0
    Last Post: 09-07-2009, 10:42 PM
  3. low level programming book
    By CChakra in forum C Programming
    Replies: 5
    Last Post: 09-08-2008, 01:23 PM
  4. Low level programming question
    By phooey in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2003, 08:59 AM
  5. loooow-level socket programming
    By skacy in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2002, 05:22 PM