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-3
    1302
  • moajoh
    1336
  • henjaa-3
    1358
  • Tim
    1386
  • melker (melsth-0)
    1434
  • jolstu-10
    1439
  • mardyh-3
    1440
  • sebbe
    1448
  • edvcal-1
    1451
  • sagand-1
    1454
  • fliqli
    1457
  • filekh-1
    1470
  • mahrus
    1474
  • joeott-1
    1481
  • rasjac-10
    1485
  • frehgl-1
    1491
  • jdupak
    1494
  • julgel-3
    1495
  • albinringstrom
    1508
  • teodur-1
    1524
  • aleblo-1
    1546
  • Aspirin (Aspirin_LTU_2023)
    1561
  • aapkii-3
    1865
  • jialim-3
    1902
  • Ludado-1
    2091
  • iskmat-1
    2091
  • guswal-1
    2242
  • alvsta-1
    2242
  • reference
    2453
  • johan
    2684
  • Alihel-1
    4454
  • jacras-1
    4520