C code examples

This commit is contained in:
mariusmonton 2018-09-19 23:51:56 +02:00
parent 7910a061bc
commit 17ac1ae411
12 changed files with 351 additions and 0 deletions

50
C/forloop/Makefile Normal file
View File

@ -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!"

25
C/forloop/dump Normal file
View File

@ -0,0 +1,25 @@
forloop.o: file format elf32-littleriscv
Disassembly of section .text:
00010074 <main>:
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 <main+0x2c>
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 <main+0x14>
100ac: 00000013 nop
100b0: 01c12403 lw s0,28(sp)
100b4: 02010113 addi sp,sp,32
100b8: 00008067 ret

8
C/forloop/forloop.c Normal file
View File

@ -0,0 +1,8 @@
void main(void) {
volatile int i;
volatile int a;
for(i=0;i<10;i++) {
a=i+5;
}
}

50
C/trace/Makefile Normal file
View File

@ -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!"

20
C/trace/dump Normal file
View File

@ -0,0 +1,20 @@
trace.o: file format elf32-littleriscv
Disassembly of section .text:
00010074 <main>:
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

7
C/trace/trace.c Normal file
View File

@ -0,0 +1,7 @@
#define TRACE (*(unsigned char *)0x40000000)
void main(void) {
TRACE = 'H';
TRACE = 'e';
}

50
C/trace2/Makefile Normal file
View File

@ -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!"

28
C/trace2/dump Normal file
View File

@ -0,0 +1,28 @@
trace2.o: file format elf32-littleriscv
Disassembly of section .text:
00010074 <main>:
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 <main+0x38>
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 <main+0x14>
100b8: 00000013 nop
100bc: 01c12403 lw s0,28(sp)
100c0: 02010113 addi sp,sp,32
100c4: 00008067 ret

9
C/trace2/trace2.c Normal file
View File

@ -0,0 +1,9 @@
#define TRACE (*(unsigned char *)0x40000000)
void main(void) {
int i;
for(i=0;i<10;i++) {
TRACE = 'a' + i;
}
}

50
C/trace3/Makefile Normal file
View File

@ -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!"

42
C/trace3/dump Normal file
View File

@ -0,0 +1,42 @@
trace3.o: file format elf32-littleriscv
Disassembly of section .text:
00010074 <main>:
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 <main+0x8c>
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 <main+0x68>
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 <main+0x44>
100f0: 00000013 nop
100f4: 02c12403 lw s0,44(sp)
100f8: 03010113 addi sp,sp,48
100fc: 00008067 ret

12
C/trace3/trace3.c Normal file
View File

@ -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];
}
}