# Lab1: RV32I Simulator
###### tags: `computer architecture`
## Convert Binary Number in a Linked List to Integer (Leetcode [1290](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/))
This is one of the “Easy” questions on LeetCode as requested in the assignment. The question is as follows.
:::info
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
Return the decimal value of the number in the linked list.
The most significant bit is at the head of the linked list.
:::
## Implementation
This is my code [here]()
### C programming
ListNode structure
```clike
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
```
```clike
int getDecimalValue(struct ListNode* head){
int ans = 0;
while(head){
ans <<= 1;
ans |= head->val;
head = head->next;
}
return ans;
}
```
### RISC-V
```
.data
list: .word 1, 0, 1
list_length: .word 3
.text
main:
jal ra, create_list
mv t1, s1 # store list head
jal ra, print_list
mv t1, s1
jal ra getDecimalValue
exit:
addi a7, x0 10 # exit
ecall
create_list:
addi sp, sp, 8
li s2, 0 # i = 0
la t1, list # list head address
la t0, list_length # to store list length
lw t0, 0(t0)
bne s2, t0, create_node
jr ra
create_node:
li s0, 0x0fff0000 # address of list head
sw ra, 0(sp) # store return address
sw s0, 4(sp) # store address of list head
jal ra malloc
lw s1, 0(t1) # load word list[i]
addi t1, t1, 4 # next element
sw s1, 0(s0) # node->value = list[i]
sw zero, 4(s0) # node->next = NULL
mv t2, s0 # prev = node
addi s2, s2, 1 # i++
addi s0, s0, 8 # one node for 8 byte
beq s2, t0, ret # chekc end condition in for loop
loop:
jal ra, malloc # get memory for next node
lw s1, 0(t1) # load word list[i]
addi t1, t1, 4 # next element
sw s1, 0(s0) # node-j>value = array[i]
sw zero, 4(s0) # node->next = NULL
sw s0, 4(t2) # prev->next = node
mv t2, s0
addi s2, s2, 1
addi s0, s0, 8
bne s2, t0, loop
ret:
lw ra, 0(sp)
lw s1, 4(sp)
addi sp, sp, 8
jr ra
malloc:
addi a0, s0, 8
li a7 214
ecall
jr ra
print_list:
beq t1, x0, end
lw a0, 0(t1)
addi a7, x0, 1
ecall
lw t1, 4(t1)
j print_list
end:
addi a0, x0, 10 # '\n' ascii
addi a7, x0, 11 # print char
ecall
jr ra
getDecimalValue:
addi sp, sp, -4
sw ra, 0(sp) # store return address
addi a0, x0, 0 # ans = 0
jal ra while # traverse linked list node
lw ra, 0(sp) # load return address
addi sp, sp, 4
jr ra
while:
beq t1, x0, end_while # if head is NULL, branch end while
lw t0, 0(t1) # t0 is node->value
lw t1, 4(t1) # load next node address
slli a0, a0, 1 # shift left 1 bit
or a0, a0, t0 # ans = ans + node->value
j while
end_while:
li a7, 1 # print ans
ecall
jr ra # return address
```
### Analysis

#### IF









