Wang Chen
2022-01-27 11:06:33 +08:00
parent ca2629bebc
commit ae10d3e698
16 changed files with 194 additions and 194 deletions

View File

@@ -20,7 +20,7 @@ stop:
nop # just for demo effect nop # just for demo effect
stack_start: stack_start:
.rept 10 .rept 12
.word 0 .word 0
.endr .endr
stack_end: stack_end:

View File

@@ -17,7 +17,7 @@ stop:
nop # just for demo effect nop # just for demo effect
stack_start: stack_start:
.rept 10 .rept 12
.word 0 .word 0
.endr .endr
stack_end: stack_end:

View File

@@ -1,59 +1,59 @@
# Calling Convention # Calling Convention
# Demo to create a leaf routine # Demo to create a leaf routine
# #
# void _start() # void _start()
# { # {
# // calling leaf routine # // calling leaf routine
# square(3); # square(3);
# } # }
# #
# int square(int num) # int square(int num)
# { # {
# return num * num; # return num * num;
# } # }
.text # Define beginning of text section .text # Define beginning of text section
.global _start # Define entry _start .global _start # Define entry _start
_start: _start:
la sp, stack_end # prepare stack for calling functions la sp, stack_end # prepare stack for calling functions
li a0, 3 li a0, 3
call square call square
# the time return here, a0 should stores the result # the time return here, a0 should stores the result
stop: stop:
j stop # Infinite loop to stop execution j stop # Infinite loop to stop execution
# int square(int num) # int square(int num)
square: square:
# prologue # prologue
addi sp, sp, -8 addi sp, sp, -8
sw s0, 0(sp) sw s0, 0(sp)
sw s1, 4(sp) sw s1, 4(sp)
# `mul a0, a0, a0` should be fine, # `mul a0, a0, a0` should be fine,
# programing as below just to demo we can contine use the stack # programing as below just to demo we can contine use the stack
mv s0, a0 mv s0, a0
mul s1, s0, s0 mul s1, s0, s0
mv a0, s1 mv a0, s1
# epilogue # epilogue
lw s0, 0(sp) lw s0, 0(sp)
lw s1, 4(sp) lw s1, 4(sp)
addi sp, sp, 8 addi sp, sp, 8
ret ret
# add nop here just for demo in gdb # add nop here just for demo in gdb
nop nop
# allocate stack space # allocate stack space
stack_start: stack_start:
.rept 10 .rept 12
.word 0 .word 0
.endr .endr
stack_end: stack_end:
.end # End of file .end # End of file

View File

@@ -1,99 +1,99 @@
# Calling Convention # Calling Convention
# Demo how to write nested routines # Demo how to write nested routines
# #
# void _start() # void _start()
# { # {
# // calling nested routine # // calling nested routine
# aa_bb(3, 4); # aa_bb(3, 4);
# } # }
# #
# int aa_bb(int a, int b) # int aa_bb(int a, int b)
# { # {
# return square(a) + square(b); # return square(a) + square(b);
# } # }
# #
# int square(int num) # int square(int num)
# { # {
# return num * num; # return num * num;
# } # }
.text # Define beginning of text section .text # Define beginning of text section
.global _start # Define entry _start .global _start # Define entry _start
_start: _start:
la sp, stack_end # prepare stack for calling functions la sp, stack_end # prepare stack for calling functions
# aa_bb(3, 4); # aa_bb(3, 4);
li a0, 3 li a0, 3
li a1, 4 li a1, 4
call aa_bb call aa_bb
stop: stop:
j stop # Infinite loop to stop execution j stop # Infinite loop to stop execution
# int aa_bb(int a, int b) # int aa_bb(int a, int b)
# return a^2 + b^2 # return a^2 + b^2
aa_bb: aa_bb:
# prologue # prologue
addi sp, sp, -16 addi sp, sp, -16
sw s0, 0(sp) sw s0, 0(sp)
sw s1, 4(sp) sw s1, 4(sp)
sw s2, 8(sp) sw s2, 8(sp)
sw ra, 12(sp) sw ra, 12(sp)
# cp and store the input params # cp and store the input params
mv s0, a0 mv s0, a0
mv s1, a1 mv s1, a1
# sum will be stored in s2 and is initialized as zero # sum will be stored in s2 and is initialized as zero
li s2, 0 li s2, 0
mv a0, s0 mv a0, s0
jal square jal square
add s2, s2, a0 add s2, s2, a0
mv a0, s1 mv a0, s1
jal square jal square
add s2, s2, a0 add s2, s2, a0
mv a0, s2 mv a0, s2
# epilogue # epilogue
lw s0, 0(sp) lw s0, 0(sp)
lw s1, 4(sp) lw s1, 4(sp)
lw s2, 8(sp) lw s2, 8(sp)
lw ra, 12(sp) lw ra, 12(sp)
addi sp, sp, 16 addi sp, sp, 16
ret ret
# int square(int num) # int square(int num)
square: square:
# prologue # prologue
addi sp, sp, -8 addi sp, sp, -8
sw s0, 0(sp) sw s0, 0(sp)
sw s1, 4(sp) sw s1, 4(sp)
# `mul a0, a0, a0` should be fine, # `mul a0, a0, a0` should be fine,
# programing as below just to demo we can contine use the stack # programing as below just to demo we can contine use the stack
mv s0, a0 mv s0, a0
mul s1, s0, s0 mul s1, s0, s0
mv a0, s1 mv a0, s1
# epilogue # epilogue
lw s0, 0(sp) lw s0, 0(sp)
lw s1, 4(sp) lw s1, 4(sp)
addi sp, sp, 8 addi sp, sp, 8
ret ret
# add nop here just for demo in gdb # add nop here just for demo in gdb
nop nop
# allocate stack space # allocate stack space
stack_start: stack_start:
.rept 10 .rept 12
.word 0 .word 0
.endr .endr
stack_end: stack_end:
.end # End of file .end # End of file

View File

@@ -1,23 +1,23 @@
# Shift Right Arithmetic Immediate # Shift Right Arithmetic Immediate
# Format: # Format:
# SLLI RD, RS1, IMM # SRAI RD, RS1, IMM
# Description: # Description:
# The immediate value determines the number of bits to shift. The contents of # The immediate value determines the number of bits to shift. The contents of
# RS1 is shifted right that many bits and the result is placed in RD. The shift # RS1 is shifted right that many bits and the result is placed in RD. The shift
# is arithmetic, i.e., the sign bit is repeatedly shifted in on the # is arithmetic, i.e., the sign bit is repeatedly shifted in on the
# most-significant end. # most-significant end.
# Comment: # Comment:
# In C, for signed integer, >> is shift right with arithmetic. # In C, for signed integer, >> is shift right with arithmetic.
.text # Define beginning of text section .text # Define beginning of text section
.global _start # Define entry _start .global _start # Define entry _start
_start: _start:
# li x6, 0x80 # x6 = 0b1000-0000 # li x6, 0x80 # x6 = 0b1000-0000
li x6, 0x80000000 # x6 = 0b1000-0000-0000-0000-0000-0000-0000-0000 li x6, 0x80000000 # x6 = 0b1000-0000-0000-0000-0000-0000-0000-0000
srai x5, x6, 4 # x5 = x6 >> 3 srai x5, x6, 4 # x5 = x6 >> 4
stop: stop:
j stop # Infinite loop to stop execution j stop # Infinite loop to stop execution
.end # End of file .end # End of file

View File

@@ -33,7 +33,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0

View File

@@ -32,7 +32,7 @@
/* /*
* POWER UP DEFAULTS * POWER UP DEFAULTS
* IER = 0: TX/RX holding register interrupts are bith disabled * IER = 0: TX/RX holding register interrupts are both disabled
* ISR = 1: no interrupt penting * ISR = 1: no interrupt penting
* LCR = 0 * LCR = 0
* MCR = 0 * MCR = 0