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:
- Insertion. Inserting new nodes at the correct spot keeping the list sorted in ascending order.
- Deletion. Deleting a node, while still keeping the list in ascending order.
- Printing in ascending order. Printing each node of the list from the first till the last one.
- 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.