file.c contains the following code:
unsigned long int a = 0;
unsigned long int b = 0;
unsigned long int c = 0;
unsigned long int d = 0;
void _start() {
a = 1;
b = 2;
c = 3;
d = 4;
return;
}
If i disassemble file.c with this command line in an Ubuntu 18.04 istance:
/opt/riscv/bin/riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32 -O0 -nostdlib -S file.c -o file.s
I get this output:
.file "quattrovariabili.c"
.option nopic
.attribute arch, "rv32i2p0"
.attribute unaligned_access, 0
.attribute stack_align, 16
.text
.globl a
.section .sbss,"aw",@nobits
.align 2
.type a, @object
.size a, 4
a:
.zero 4
.globl b
.align 2
.type b, @object
.size b, 4
b:
.zero 4
.globl c
.align 2
.type c, @object
.size c, 4
c:
.zero 4
.globl d
.align 2
.type d, @object
.size d, 4
d:
.zero 4
.text
.align 2
.globl _start
.type _start, @function
_start:
addi sp,sp,-16
sw s0,12(sp)
addi s0,sp,16
lui a5,%hi(a)
li a4,1
sw a4,%lo(a)(a5)
lui a5,%hi(b)
li a4,2
sw a4,%lo(b)(a5)
lui a5,%hi(c)
li a4,3
sw a4,%lo(c)(a5)
lui a5,%hi(d)
li a4,4
sw a4,%lo(d)(a5)
nop
lw s0,12(sp)
addi sp,sp,16
jr ra
.size _start, .-_start
.ident "GCC: (GNU) 10.2.0"
If i compile the file.c in file.elf and then i disassemble file.elf in file.s i get:
quattrovariabili.elf: file format elf32-littleriscv
Disassembly of section .text:
00010074 <_start> (File Offset: 0x74):
_start():
10074: ff010113 addi sp,sp,-16
10078: 00812623 sw s0,12(sp)
1007c: 01010413 addi s0,sp,16
10080: 000117b7 lui a5,0x11
10084: 00100713 li a4,1
10088: 0ae7ac23 sw a4,184(a5) # 110b8 <__DATA_BEGIN__> (File Offset: 0x10b8)
1008c: 000117b7 lui a5,0x11
10090: 00200713 li a4,2
10094: 0ae7ae23 sw a4,188(a5) # 110bc <b> (File Offset: 0x10bc)
10098: 00300713 li a4,3
1009c: 80e1a423 sw a4,-2040(gp) # 110c0 <c> (File Offset: 0x10c0)
100a0: 00400713 li a4,4
100a4: 80e1a623 sw a4,-2036(gp) # 110c4 <d> (File Offset: 0x10c4)
100a8: 00000013 nop
100ac: 00c12403 lw s0,12(sp)
100b0: 01010113 addi sp,sp,16
100b4: 00008067 ret
Disassembly of section .sbss:
000110b8 <a> (File Offset: 0xb8):
__bss_start():
110b8: 0000 unimp
...
000110bc <b> (File Offset: 0xbc):
110bc: 0000 unimp
...
000110c0 <c> (File Offset: 0xc0):
110c0: 0000 unimp
...
000110c4 <d> (File Offset: 0xc4):
110c4: 0000 unimp
...
Disassembly of section .comment:
00000000 <.comment> (File Offset: 0xb8):
0: 3a434347 fmsub.d ft6,ft6,ft4,ft7,rmm
4: 2820 fld fs0,80(s0)
6: 29554e47 fmsub.s ft8,fa0,fs5,ft5,rmm
a: 3120 fld fs0,96(a0)
c: 2e30 fld fa2,88(a2)
e: 2e32 fld ft8,264(sp)
10: 0030 addi a2,sp,8
Disassembly of section .riscv.attributes:
00000000 <.riscv.attributes> (File Offset: 0xca):
0: 1b41 addi s6,s6,-16
2: 0000 unimp
4: 7200 flw fs0,32(a2)
6: 7369 lui t1,0xffffa
8: 01007663 bgeu zero,a6,14 <_start-0x10060> (File Offset: 0xde)
c: 0011 c.nop 4
e: 0000 unimp
10: 1004 addi s1,sp,32
12: 7205 lui tp,0xfffe1
14: 3376 fld ft6,376(sp)
16: 6932 flw fs2,12(sp)
18: 7032 flw ft0,44(sp)
1a: 0030 addi a2,sp,8
Why the following part is different? The elf file does not work on my ISA simulator since the store is performed at illegal address.
From the first s file:
lui a5,%hi(c)
li a4,3
sw a4,%lo(c)(a5)
lui a5,%hi(d)
li a4,4
sw a4,%lo(d)(a5)
From the second s file:
10098: 00300713 li a4,3
1009c: 80e1a423 sw a4,-2040(gp) # 110c0 <c> (File Offset: 0x10c0)
100a0: 00400713 li a4,4
100a4: 80e1a623 sw a4,-2036(gp) # 110c4 <d> (File Offset: 0x10c4)