How to navigate within heap in RISC-V ASM?

  Kiến thức lập trình

I’m practicing my ASM skills in RISC-V, because I already studied it in school. I want to move through a node network stored in heap. I want to create new nodes in heap and navigate through them, but I’m having difficulties changing the heap address to store new nodes.

A node is simply data, path1, path2. Depending on the data the next destination node would change.

Usually I would simply do addi code_address, code_address, 1 but I’m getting errors like this:
Error: Runtime exception at 0x004004d0: address out of range 0x00000000

Can I get advice on navigation though objects (or nodes in this case) within the heap?

    li  syscall_id, sbrk   # System call for memory allocation
    li  bytes_to_alloc, heap_size   # Number of bytes to allocate
    mv reg_1, bytes_to_alloc       # Store the heap size in reg_1
    ecall   # System call to allocate memory
    ret     # Return from function


addEntry:
    
    la reg_2, rootNode
    mv reg_3, code_address       # Load code address
    mv reg_4, clear_text_address       # Load clear text address
    mv reg_5, clear_char          # Load clear text character
    li reg_6, '?'   # ASCII for '?'

addEntry_loop:
    lbu reg_7, 0(reg_4)   # Load a character from the clear text
    beqz reg_7, end_loop   # If the character is zero, end the loop
    
    li reg_8, '*'   # ASCII for '*'
    li reg_9, '-'   # ASCII for '-'
    beq reg_7, reg_8, star_case   # If the character is '*', go to star_case
    beq reg_7, reg_9, dash_case  # If the character is '-', go to dash_case

    addi reg_4, reg_4, 1  # Move to the next character
    j addEntry_loop  # Continue the loop

star_case:
    beqz reg_2, end_addEntry   # If root is null, end addition
    mv reg_10, reg_2
    ld reg_2, 8(reg_2)   # Load the next node for '.'
    addi reg_4, reg_4, 1  # Move to the next character
    j addEntry_loop  # Jump to loop

dash_case:
    beqz reg_2, end_addEntry   # If root is null, end addition
    mv reg_10, reg_2
    ld reg_2, 16(reg_2)   # Load the next node for '-'
    addi reg_4, reg_4, 1  # Move to the next character
    j addEntry_loop  # Jump to loop
    
end_addEntry:

    mv output, reg_2   # Store the output
    li syscall_exit, 1
    ecall   # System call for exit
    li syscall_exit, 10
    ecall   # System call to halt execution

New contributor

rcp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT