You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.6 KiB
40 lines
1.6 KiB
# Function input
|
|
addi sp, sp, -16 # Reserve stack
|
|
sw ra, 12(sp) # Save registers
|
|
sw fp, 8(sp)
|
|
sw s1, 4(sp)
|
|
addi fp, sp, 16 # Set fp
|
|
|
|
li a0, @fill_line_buffer # Fill the internal line buffer.
|
|
ecall
|
|
bgez a0, input_nonempty # More input found
|
|
la a0, $str$prototype # EOF: Return empty string.
|
|
j input_done
|
|
|
|
input_nonempty:
|
|
mv s1, a0
|
|
addi t0, s1, 5 # Compute bytes for string (+NL+NUL),
|
|
addi t0, t0, @.__str__ # Including header.
|
|
srli a1, t0, 2 # Convert to words.
|
|
la a0, $str$prototype # Load address of string prototype.
|
|
jal ra, alloc2 # Allocate string.
|
|
sw s1, @.__len__(a0) # Store string length.
|
|
mv a2, s1 # Pass length.
|
|
mv s1, a0 # Save string object address.
|
|
addi a1, a0, @.__str__ # Pass address of string data.
|
|
li a0, @read_string # ecall to read from internal buffer.
|
|
ecall
|
|
addi a0, a0, 1 # Actual length (including NL).
|
|
sw a0, @.__len__(s1) # Store actual length.
|
|
add t0, a0, s1
|
|
li t1, 10 # Store newline and null byte
|
|
sb t1, @.__str__-1(t0)
|
|
sb zero, @.__str__(t0) # Store null byte at end.
|
|
mv a0, s1 # Return string object.
|
|
|
|
input_done:
|
|
lw s1, -12(fp)
|
|
lw ra, -4(fp)
|
|
lw fp, -8(fp)
|
|
addi sp, sp, 16
|
|
jr ra |