We are using a raspberry pi in my computer systems class. We need to Write a statically allocated linked list in ARM assembly.
Steps You Should Follow
Step 1
You will statically allocate some space for your link list elements. You need to make 5 elements, each elements should have two components, the next pointer first then the value stored. First inialize next pointer to NULL. The value stored at the address will be a string pointer. It doesn't matter which values you use
Step 2
You will then write a function that links the statically alloacted linked list elements together.
Step 3
In your main you call your linking function to link all 5 elements together.
Step 4
Print out each emement in order by traversing the list. This means that you can't store each address in registers. You will lose points if you just print everything.
Extra credit
Use the malloc call to allacte for n (any number of entries represented by the letter n) entries from the command line.
Source Code:
extern malloc
extern free
extern putchar
extern puts
section .data
size_i: ; Used to determine the size of the structure
struc node
info: resd 1
next: resd 1
endstruc
len: equ $ - size_i ; Size of the data type
nullMes: db 'Null pointer - the list is empty', 0
addMes: db 'Adding a new element', 0
printMes: db 'Printing a linked list:', 0
cleanMes: db 'Cleaning an element', 0
doneCleanMes: db 'Ready for cleaning...', 0
emptyListMes: db '- empty list -', 0
section .bss
prim: resd 1
append:
push ebp ; Save the stack
mov ebp, esp
push eax ; Save the registers
push ebx
push len ; Size to get from the heap and pass the size to the malloc function
call malloc ; Call the malloc function - now eax has the address of the allocated memory
mov ebx, [ebp + 12]
mov [eax + info], ebx ; Add the element to the node data field
mov dword [eax + next], 0 ; Address of the next element is NULL, because it is the last element in the list
mov ebx, [ebp + 8] ; Retrieve the address to the first element
cmp dword [ebx], 0
je null_pointer
mov ebx, [ebx] ; This parameter was the address of the address
; Now it is the address of the first element, in this case, not null
; If it is not NULL, find the address of the last element
next_element:
cmp dword [ebx + next], 0
je found_last
mov ebx, [ebx + next]
jmp next_element
found_last:
push eax
push addMes
call puts
add esp, 4 ; Restore the stack
pop eax
mov [ebx + next], eax ; Last element is this one from the newly allocated memory block
go_out:
pop ebx ; Restore registers
pop eax
mov esp, ebp
pop ebp
ret 8 ; Return to the caller function and cleaning the stack
null_pointer:
push eax
push nullMes
call puts
add esp, 4
pop eax
mov [ebx], eax ; Point the address of the first element to the allocated memory
jmp go_out
print:
push ebp
mov ebp, esp
push ebx
mov ebx, [ebp + 8] ; Address of the first element
cmp ebx, 0
je emptyList
push eax
push printMes ; Print message "Printing a linked list"
call puts
add esp, 4
pop eax
Note: I've added comments for your understanding, and I've used malloc() in the program.
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================
Get Answers For Free
Most questions answered within 1 hours.