Simple Calculator
Implement a program, that loads two numbers from the serial port, adds them together and prints them to the serial port.
The C language program execution requires at least a minimal startup code sequence,
even for the simplest bare-metal environment.
The C code ABI for RISC-V requires at least to set global pointer (gp
) register,
see the file crt0local.S.
The file crt0local.S
will be included in the execution directory and linked with your program.
The complete task description can also be found here, Makefile and template files for your own testing can be found on GitLab.
Makefile (click)
ARCH=riscv64-unknown-elf
#ARCH=riscv64-linux-gnu
SOURCES = submission.c crt0local.S
TARGET_EXE = submission
CC=$(ARCH)-gcc
CXX=$(ARCH)-g++
AS=$(ARCH)-as
LD=$(ARCH)-ld
OBJCOPY=$(ARCH)-objcopy
ARCHFLAGS += -mabi=ilp32
ARCHFLAGS += -march=rv32i
ARCHFLAGS += -fno-lto
CFLAGS += -ggdb -Os -Wall
CXXFLAGS+= -ggdb -Os -Wall
AFLAGS += -ggdb
LDFLAGS += -ggdb
LDFLAGS += -nostartfiles
LDFLAGS += -nostdlib
LDFLAGS += -static
#LDFLAGS += -specs=/opt/musl/riscv64-linux-gnu/lib/musl-gcc.specs
LOADLIBES += -lgcc
CFLAGS += $(ARCHFLAGS)
CXXFLAGS+= $(ARCHFLAGS)
AFLAGS += $(ARCHFLAGS)
LDFLAGS += $(ARCHFLAGS)
OBJECTS += $(filter %.o,$(SOURCES:%.S=%.o))
OBJECTS += $(filter %.o,$(SOURCES:%.c=%.o))
OBJECTS += $(filter %.o,$(SOURCES:%.cpp=%.o))
all : default
.PHONY : default clean dep all run_test
%.o:%.S
$(CC) -D__ASSEMBLY__ $(AFLAGS) -c $< -o $@
%.o:%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
%.o:%.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $<
%.s:%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -S $< -o $@
default : submission
$(TARGET_EXE) : $(OBJECTS)
$(CC) $(LDFLAGS) $^ $(LOADLIBES) -o $@
dep: depend
depend: $(SOURCES) $(glob *.h)
echo '# autogenerated dependencies' > depend
ifneq ($(filter %.S,$(SOURCES)),)
$(CC) -D__ASSEMBLY__ $(AFLAGS) -w -E -M $(filter %.S,$(SOURCES)) >> depend
endif
ifneq ($(filter %.c,$(SOURCES)),)
$(CC) $(CFLAGS) $(CPPFLAGS) -w -E -M $(filter %.c,$(SOURCES)) >> depend
endif
ifneq ($(filter %.cpp,$(SOURCES)),)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -w -E -M $(filter %.cpp,$(SOURCES)) >> depend
endif
clean:
rm -f *.o *.a $(OBJECTS) $(TARGET_EXE) depend
-include depend
Additional files present at compile time:
crt0local.S (click)
/* minimal replacement of crt0.o which is else provided by C library */
.globl main
.globl _start
.globl __start
.option norelax
.text
__start:
_start:
.option push
.option norelax
la gp, __global_pointer$
.option pop
la sp, __stack_end
addi a0, zero, 0
addi a1, zero, 0
jal main
quit:
addi a0, zero, 0
addi a7, zero, 93 /* SYS_exit */
ecall
loop: ebreak
beq zero, zero, loop
.bss
__stack_start:
.skip 4096
__stack_end:
.end _start
Input: Simple calculator.
In:
Two numbers present at the serial port.
Out:
Their sum to the serial port.
Your program will be run with the following arguments:
--dump-cycles --cycle-limit 5000 submission
Your program will be scored by this following metric:
Runtime of the program in cycles.
For interactive solution, you can use QtRvSim. The web version of the simulator is located here.
Top Scores (cycles)
-
emisee-31302
-
moajoh1336
-
henjaa-31358
-
Tim1386
-
melker (melsth-0)1434
-
jolstu-101439
-
mardyh-31440
-
sebbe1448
-
edvcal-11451
-
sagand-11454
-
fliqli1457
-
filekh-11470
-
mahrus1474
-
joeott-11481
-
rasjac-101485
-
frehgl-11491
-
jdupak1494
-
julgel-31495
-
albinringstrom1508
-
teodur-11524
-
aleblo-11546
-
Aspirin (Aspirin_LTU_2023)1561
-
aapkii-31865
-
jialim-31902
-
Ludado-12091
-
iskmat-12091
-
guswal-12242
-
alvsta-12242
-
reference2453
-
johan2684
-
Alihel-14454
-
jacras-14520