From 17ac1ae411825a357dcc4feffe8b43f40dfe27c2 Mon Sep 17 00:00:00 2001 From: mariusmonton Date: Wed, 19 Sep 2018 23:51:56 +0200 Subject: [PATCH] C code examples --- C/forloop/Makefile | 50 +++++++++++++++++++++++++++++++++++++++++++++ C/forloop/dump | 25 +++++++++++++++++++++++ C/forloop/forloop.c | 8 ++++++++ C/trace/Makefile | 50 +++++++++++++++++++++++++++++++++++++++++++++ C/trace/dump | 20 ++++++++++++++++++ C/trace/trace.c | 7 +++++++ C/trace2/Makefile | 50 +++++++++++++++++++++++++++++++++++++++++++++ C/trace2/dump | 28 +++++++++++++++++++++++++ C/trace2/trace2.c | 9 ++++++++ C/trace3/Makefile | 50 +++++++++++++++++++++++++++++++++++++++++++++ C/trace3/dump | 42 +++++++++++++++++++++++++++++++++++++ C/trace3/trace3.c | 12 +++++++++++ 12 files changed, 351 insertions(+) create mode 100644 C/forloop/Makefile create mode 100644 C/forloop/dump create mode 100644 C/forloop/forloop.c create mode 100644 C/trace/Makefile create mode 100644 C/trace/dump create mode 100644 C/trace/trace.c create mode 100644 C/trace2/Makefile create mode 100644 C/trace2/dump create mode 100644 C/trace2/trace2.c create mode 100644 C/trace3/Makefile create mode 100644 C/trace3/dump create mode 100644 C/trace3/trace3.c diff --git a/C/forloop/Makefile b/C/forloop/Makefile new file mode 100644 index 0000000..ed1360b --- /dev/null +++ b/C/forloop/Makefile @@ -0,0 +1,50 @@ +TARGET = trace + +TARGET_ARCH=riscv32 + +CC = riscv32-unknown-linux-gnu-gcc +# compiling flags here +#CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 + +LINKER = riscv32-unknown-linux-gnu-gcc +# linking flags here +LFLAGS = -I. +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.c) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) +# $(LINKER) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBDIR) -o $@ + riscv32-unknown-linux-gnu-objdump -d $< > dump + objcopy -Oihex $< $(TARGET).hex +# @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c + @echo "Compiling "$<" ..." +# $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + $(CC) $(CFLAGS) $(INCDIR) $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/C/forloop/dump b/C/forloop/dump new file mode 100644 index 0000000..57df0e5 --- /dev/null +++ b/C/forloop/dump @@ -0,0 +1,25 @@ + +forloop.o: file format elf32-littleriscv + + +Disassembly of section .text: + +00010074
: + 10074: fe010113 addi sp,sp,-32 + 10078: 00812e23 sw s0,28(sp) + 1007c: 02010413 addi s0,sp,32 + 10080: fe042623 sw zero,-20(s0) + 10084: 01c0006f j 100a0 + 10088: fec42783 lw a5,-20(s0) + 1008c: 00578793 addi a5,a5,5 + 10090: fef42423 sw a5,-24(s0) + 10094: fec42783 lw a5,-20(s0) + 10098: 00178793 addi a5,a5,1 + 1009c: fef42623 sw a5,-20(s0) + 100a0: fec42703 lw a4,-20(s0) + 100a4: 00900793 li a5,9 + 100a8: fee7d0e3 ble a4,a5,10088 + 100ac: 00000013 nop + 100b0: 01c12403 lw s0,28(sp) + 100b4: 02010113 addi sp,sp,32 + 100b8: 00008067 ret diff --git a/C/forloop/forloop.c b/C/forloop/forloop.c new file mode 100644 index 0000000..65b50f1 --- /dev/null +++ b/C/forloop/forloop.c @@ -0,0 +1,8 @@ +void main(void) { + volatile int i; + volatile int a; + + for(i=0;i<10;i++) { + a=i+5; + } +} diff --git a/C/trace/Makefile b/C/trace/Makefile new file mode 100644 index 0000000..ed1360b --- /dev/null +++ b/C/trace/Makefile @@ -0,0 +1,50 @@ +TARGET = trace + +TARGET_ARCH=riscv32 + +CC = riscv32-unknown-linux-gnu-gcc +# compiling flags here +#CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 + +LINKER = riscv32-unknown-linux-gnu-gcc +# linking flags here +LFLAGS = -I. +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.c) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) +# $(LINKER) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBDIR) -o $@ + riscv32-unknown-linux-gnu-objdump -d $< > dump + objcopy -Oihex $< $(TARGET).hex +# @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c + @echo "Compiling "$<" ..." +# $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + $(CC) $(CFLAGS) $(INCDIR) $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/C/trace/dump b/C/trace/dump new file mode 100644 index 0000000..8b42e14 --- /dev/null +++ b/C/trace/dump @@ -0,0 +1,20 @@ + +trace.o: file format elf32-littleriscv + + +Disassembly of section .text: + +00010074
: + 10074: ff010113 addi sp,sp,-16 + 10078: 00812623 sw s0,12(sp) + 1007c: 01010413 addi s0,sp,16 + 10080: 400007b7 lui a5,0x40000 + 10084: 04800713 li a4,72 + 10088: 00e78023 sb a4,0(a5) # 40000000 <__global_pointer$+0x3ffee758> + 1008c: 400007b7 lui a5,0x40000 + 10090: 06500713 li a4,101 + 10094: 00e78023 sb a4,0(a5) # 40000000 <__global_pointer$+0x3ffee758> + 10098: 00000013 nop + 1009c: 00c12403 lw s0,12(sp) + 100a0: 01010113 addi sp,sp,16 + 100a4: 00008067 ret diff --git a/C/trace/trace.c b/C/trace/trace.c new file mode 100644 index 0000000..7597464 --- /dev/null +++ b/C/trace/trace.c @@ -0,0 +1,7 @@ + +#define TRACE (*(unsigned char *)0x40000000) + +void main(void) { + TRACE = 'H'; + TRACE = 'e'; +} diff --git a/C/trace2/Makefile b/C/trace2/Makefile new file mode 100644 index 0000000..6e98f6d --- /dev/null +++ b/C/trace2/Makefile @@ -0,0 +1,50 @@ +TARGET = trace2 + +TARGET_ARCH=riscv32 + +CC = riscv32-unknown-linux-gnu-gcc +# compiling flags here +#CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 + +LINKER = riscv32-unknown-linux-gnu-gcc +# linking flags here +LFLAGS = -I. +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.c) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) +# $(LINKER) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBDIR) -o $@ + riscv32-unknown-linux-gnu-objdump -d $< > dump + objcopy -Oihex $< $(TARGET).hex +# @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c + @echo "Compiling "$<" ..." +# $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + $(CC) $(CFLAGS) $(INCDIR) $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/C/trace2/dump b/C/trace2/dump new file mode 100644 index 0000000..805608f --- /dev/null +++ b/C/trace2/dump @@ -0,0 +1,28 @@ + +trace2.o: file format elf32-littleriscv + + +Disassembly of section .text: + +00010074
: + 10074: fe010113 addi sp,sp,-32 + 10078: 00812e23 sw s0,28(sp) + 1007c: 02010413 addi s0,sp,32 + 10080: fe042623 sw zero,-20(s0) + 10084: 0280006f j 100ac + 10088: fec42783 lw a5,-20(s0) + 1008c: 0ff7f713 andi a4,a5,255 + 10090: 400007b7 lui a5,0x40000 + 10094: 06170713 addi a4,a4,97 + 10098: 0ff77713 andi a4,a4,255 + 1009c: 00e78023 sb a4,0(a5) # 40000000 <__global_pointer$+0x3ffee738> + 100a0: fec42783 lw a5,-20(s0) + 100a4: 00178793 addi a5,a5,1 + 100a8: fef42623 sw a5,-20(s0) + 100ac: fec42703 lw a4,-20(s0) + 100b0: 00900793 li a5,9 + 100b4: fce7dae3 ble a4,a5,10088 + 100b8: 00000013 nop + 100bc: 01c12403 lw s0,28(sp) + 100c0: 02010113 addi sp,sp,32 + 100c4: 00008067 ret diff --git a/C/trace2/trace2.c b/C/trace2/trace2.c new file mode 100644 index 0000000..18274f7 --- /dev/null +++ b/C/trace2/trace2.c @@ -0,0 +1,9 @@ + +#define TRACE (*(unsigned char *)0x40000000) + +void main(void) { + int i; + for(i=0;i<10;i++) { + TRACE = 'a' + i; + } +} diff --git a/C/trace3/Makefile b/C/trace3/Makefile new file mode 100644 index 0000000..0abc24f --- /dev/null +++ b/C/trace3/Makefile @@ -0,0 +1,50 @@ +TARGET = trace3 + +TARGET_ARCH=riscv32 + +CC = riscv32-unknown-linux-gnu-gcc +# compiling flags here +#CFLAGS = -Wall -I. -O0 -nostartfiles -march=rv32i -mabi=ilp32 +CFLAGS = -Wall -I. -O0 -nostdlib -march=rv32i -mabi=ilp32 + +LINKER = riscv32-unknown-linux-gnu-gcc +# linking flags here +LFLAGS = -I. +LIBS = $(EXTRA_LIBS) + + +# change these to proper directories where each file should be +SRCDIR = ./ +OBJDIR = . +BINDIR = ./ +INCDIR = -I. +LIBDIR = -L. + + +SOURCES := $(wildcard $(SRCDIR)/*.c) +INCLUDES := $(wildcard $(INCDIR)/*.h) +OBJECTS := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o) +rm = rm -f + + +$(BINDIR)/$(TARGET): $(OBJECTS) +# $(LINKER) $(OBJECTS) $(LFLAGS) $(LIBS) $(LIBDIR) -o $@ + riscv32-unknown-linux-gnu-objdump -d $< > dump + objcopy -Oihex $< $(TARGET).hex +# @echo "Linking complete!" + +$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c + @echo "Compiling "$<" ..." +# $(CC) $(CFLAGS) $(INCDIR) -c $< -o $@ + $(CC) $(CFLAGS) $(INCDIR) $< -o $@ + @echo "Done!" + +.PHONY: clean +clean: + @$(rm) $(OBJECTS) + @echo "Cleanup complete!" + +.PHONY: remove +remove: clean + @$(rm) $(BINDIR)/$(TARGET) + @echo "Executable removed!" diff --git a/C/trace3/dump b/C/trace3/dump new file mode 100644 index 0000000..7292827 --- /dev/null +++ b/C/trace3/dump @@ -0,0 +1,42 @@ + +trace3.o: file format elf32-littleriscv + + +Disassembly of section .text: + +00010074
: + 10074: fd010113 addi sp,sp,-48 + 10078: 02812623 sw s0,44(sp) + 1007c: 03010413 addi s0,sp,48 + 10080: 000107b7 lui a5,0x10 + 10084: 1007a603 lw a2,256(a5) # 10100 + 10088: 10078713 addi a4,a5,256 + 1008c: 00472683 lw a3,4(a4) + 10090: 10078713 addi a4,a5,256 + 10094: 00872703 lw a4,8(a4) + 10098: fcc42e23 sw a2,-36(s0) + 1009c: fed42023 sw a3,-32(s0) + 100a0: fee42223 sw a4,-28(s0) + 100a4: 10078793 addi a5,a5,256 + 100a8: 00c7c783 lbu a5,12(a5) + 100ac: fef40423 sb a5,-24(s0) + 100b0: fe042623 sw zero,-20(s0) + 100b4: 0280006f j 100dc + 100b8: 400007b7 lui a5,0x40000 + 100bc: fec42703 lw a4,-20(s0) + 100c0: ff040693 addi a3,s0,-16 + 100c4: 00e68733 add a4,a3,a4 + 100c8: fec74703 lbu a4,-20(a4) + 100cc: 00e78023 sb a4,0(a5) # 40000000 <__global_pointer$+0x3ffee6f3> + 100d0: fec42783 lw a5,-20(s0) + 100d4: 00178793 addi a5,a5,1 + 100d8: fef42623 sw a5,-20(s0) + 100dc: fec42783 lw a5,-20(s0) + 100e0: ff040713 addi a4,s0,-16 + 100e4: 00f707b3 add a5,a4,a5 + 100e8: fec7c783 lbu a5,-20(a5) + 100ec: fc0796e3 bnez a5,100b8 + 100f0: 00000013 nop + 100f4: 02c12403 lw s0,44(sp) + 100f8: 03010113 addi sp,sp,48 + 100fc: 00008067 ret diff --git a/C/trace3/trace3.c b/C/trace3/trace3.c new file mode 100644 index 0000000..8ce1430 --- /dev/null +++ b/C/trace3/trace3.c @@ -0,0 +1,12 @@ +#define TRACE (*(unsigned char *)0x40000000) + + + +void main(void) { + char msg[] = "Hello world\n"; + int i; + + for (i=0; msg[i] != '\0'; i++) { + TRACE = msg[i]; + } +}