first computer is ENIAC,built in 1946,mainly used for calculating triangle.
army computer to programmer,mostly are women.
von Neumann architecture: stored program computer.
the first stored program computer is EDVAC,built in 1949,剑桥。
Consequence: Everything has a memeory address.Every instruction and data must be stored in memory, and we can get them by their address because all things can be represented by binary numbers.
Most instructions are represented as 32-bit numbers in RISC-V ,and that is same in RV32,RV64,RV128.
and we divide the 32 bits into several fields,each field tells processor something about instruction.如果直接使用32位来表示我们的指令,那么会有2^32种不同的指令,这显然是不现实的,RISC-V直接定义了6种基础的指令格式,包括R-type,I-type,S-type,B-type,U-type和J-type,每种格式都有自己特定的字段划分方式。
Instruction Formats
R-type reg-reg operations
rs1(source register 1): specifies register containing first operand.
rs2: specifies register containing second operand.
rd(Desination register): specifies register which will receive result of computation.
each register field holds a 5-bit number,刚好32个寄存器。
luird,imm20# load upper immediate and set lower 12 bits to 0auipc# add upper immediate to pcanexample:howtoset0xDEADBEEFtox10luix10,0xDEADBaddix10,x10,0xEEF=>x10=0xDEADAEEFwhy?因为addi会进行符号扩展,所以0xEEF会被扩展成0xFFFFEEF,相当于B-1那就是0xDEADAEEF,所以我们是不是可以使用addiu?RISC-V有这个东西吗,从前面的I-type可以看到所有的func3都被用完了,所以没地方了,那么怎么办呢?只能在lui的时候提前加上1,这样就抵消了。为此有了一个新的指令:lird,imm32# 这个指令会自动帮你分成lui和addi两条指令来执行,并且自动加一保证结果的正确。至于auipc它的格式一样,auipcrd,imm20将imm20左移12位加上pc的值然后存到rd中。
write pc+4 to rd
set pc = (rs + imm) & ~1 (注意最后还是要保证偶数的)
由于实际使用的是I-type,所以最低位并不是0!
对了这个pc是绝对地址欧!
some examples:
# ret and jr psuedo-instructions
ret = jr ra = jalr x0,ra,0
# call function at any 32-bit absolute address
lui x1, <hi20bits>
jalr ra, x1, <lo12bits>
# Jump PC-relative with 32-bit offset
auipc x1, <hi20bits> # x1 = pc + offset_high
jalr x0, x1, <lo12bits> # pc = x1 + offset_low