Hello there guys! I must implement a linked list in assembly (using MIPS). We're supposed to dynamically allocate memory for the nodes. This is what I am asked to do:
Write a program in assembly (MIPS) that implements a link-list. The client can choose among the following options. So the program should include:

  1. Insertion. Inserting new nodes at the correct spot keeping the list sorted in ascending order.
  2. Deletion. Deleting a node, while still keeping the list in ascending order.
  3. Printing in ascending order. Printing each node of the list from the first till the last one.
  4. Recursive printing in descending order.

On the instructions I was given it says that we should allocate memory as:

li $a0, 8
li $v0, 9
syscall

move $t1, $v0

The first byte contains the data of an integer inserted by the customer and the 2nd contains a link to the next node. etc.

This is what I have done so far.

.data
options: .asciiz " What would you like to do? \n2.Insert \n3.Print \nPlease insert a number below and press enter. "
output_sorted_list: .asciiz "lalalal"
first: .word 0
insertmsg: .asciiz "Enter integer pls"
printmsg:   .asciiz "end of printing"

.text

main:

optionMenu:

    la $a0, options
    li  $v0, 4
    syscall
    li $v0, 5
    syscall
    move $t0, $v0

    #beq $t0,1, exit
    beq $t0, 2, insert
    beq $t0, 3, print
    #beq $t0, 4, ascending
    #beq $t0, 5, descending

insert: j addnode
        j optionMenu
addnode:
        #creating linked list
        li $a0,8
        li $v0, 9
        syscall
        move $t1, $v0

        #copying the pointer to first

        sw $t1, first

        la $a0, insertmsg
        li  $v0, 4
        syscall
        li $v0, 5
        syscall
        sw $v0, ($t1)

        sw $0, 4($t1)

        #print
        lw $a0, ($t1)
        li $v0, 1
        syscall

        lw $a0, 4($t1)
        li $v0, 1
        syscall
        j optionMenu

print:  lui $t1, 0x1001
        addi $t1, $t1, 0
        lw $a0, 0($t1)
        addi $v0, $0, 1
        syscall

        la $a0, printmsg
        li  $v0, 4
        syscall
        jr print
#printloop:
        #beqz $t1, endloop

#       lw $a0, 0($t1)
#       li $a0, 1
#       syscall

#       lw $t1, 4($t1)
#       jr printloop

endloop:
        la $a0, printmsg
        li  $v0, 4
        syscall

        #la $t0, 0($t1)
        #la $s0, 0($t1)
        #la $a0, insertmsg
        #li $v0, 1
        #syscall
        #lw $s0, 4($t1)
        #bne $s0, $zero, print

        li $v0, 1
        syscall

        li $v0, 10
        syscall

My program is just inesrting elements so far, since I do not know how to do it correctly, plus I am stuck because I keep failing on printing...
Any help would be really appreciated guys.
Thank you in advance.

Recommended Answers

All 3 Replies

Sadly you left out what OS is running. Few will guess that detail but my bet this is your homework. Not that you can't ask but you have to give enough detail in order to hope for a good answer.

Please read https://www.daniweb.com/programming/threads/435023/read-this-before-posting-a-question

That out of the way, I never bothered with this in assembler. For all the work I've done the assembler is for device drivers, board support packages and for the lists and such, we move on to C for the target system.

Hello, unfortunately I could not reach you the past time. It runs in Windows 10. I understand what you mean and therefore I will try my best!
I will explain my thinking process and my code. So basically, the program starts with a Menu that lets the user choose what they would like to do. The Menu list is supposed to be: Insertion of an element, deletion of an element, printing the list in ascending order and lastly printing the list in descending order. Optionally, it should have a last choice to exit the system.
I started off with the first option, Insertion of an element in the list. As I was asked, I need to make space in the memory for each entry. This is done in lines 31-32. It is space for 2 integers, since every inserted element should have a value and a node to be linked.
I thought I could insert the value 0 to the 2nd part of the memory of a node.
Initially, I tried only inserting elements and printing them so that I can see my code is working, even though I'm asked to insert elements(in ascending order!).
My first problem came up when I wanted to print 2 inserted items in the list. Printing is apparently involved in this.
I have not gone much into the other sections of the code sadly because I am stuck here.
Are my thoughts right so far?

Same thoughts plus one. IRL or in real life we rarely write the entire thing in assembler. That's only done in homework. At the office we get a C compiler now to make the project maintainable for decades. Also, it's more cost effective.

The second new thought is to pull apart the problem to this, that and the other thing. Divide and conquer. As this is some MIPS board (I'm guessing as you didn't reveal) the output and such and the code only exists in that system. No one ever created a unified system where you write Mips and all those that follow the discussion know what to code to produce input and output.

That's why this is going to be something you have to create. My advice is to work the problem in small steps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.