CO C2 Instructions

Introduction

  • Instructions and Instruction set are Language of the computer
  • The Design goals are:
  • Maximize performance
  • Minimize cost
  • Reduce design time

Our chosen instruction set is RISC-V.

Instruction characteristics

  • Type of internal storage in processer, e.g Regs

    • Stack
    • Accumulator
    • General purpose register
      • Register-Memory
      • Register-Register:load/store
  • The number of the memory operand in the instruction

    Instruction Type Maximum number of operands Number of memory addresses
    Register-Register 3 0
    Register-memory 2 1
    Memory-memory 2 / 3 2 / 3
  • Operations in the instruction Set

  • Type, e.g Reg/Memory and Size of Operands e.g 32/64

  • Representation in the Computer: Encoding

Variables difference

  • in C : int, char, float
  • in Instruction Set :
    • Register
    • Memory address with Displacement ,Immediate
    • Stack

Design Principle

  1. Simplicity favors regularity.
  2. Smaller is faster.
  3. Make the common case fast.

Operations and Operands of the Computer Hardware

Operation

Every computer must be able to perform arithmetic:

  • Only one operation per instruction;
  • Exactly three variables / operands.
1
2
add a,b,c  #a <- b + c
sub a,b,c #a <- b - c

Operand

Register Operands

  • Arithmetic instructions operands must be registers or immediate.

  • 32 registers in RISC-V: x0 - x31, 32 bits(word) /64 bits(doubleword) for each.

    Name Register Name Usage Preserved On call?
    x0 0 The constant value 0 n.a
    x1(ra) 1 Return address(link register) y
    x2(sp) 2 Stack pointer y
    x3(gp) 3 Global pointer y
    x4(tp) 4 Thread pointer y
    x5-x7 5-7 Temporaries n
    x8-x9 8-9 Saved y
    x10-x17 10-17 Arguments/results n
    x18-x27 18-27 Saved y
    x28-x31 28-31 Temporaries n

寄存器个数的增加会导致指令中表示寄存器的bit增加。

Memory Operands

  • save much more data and complex data structures

  • Data transfer instructions: load,store

    1
    2
    3
    4
    5
    ld a,i(b)  #from memory b[i] to register a, 64 bits 
    lw c,i(d) #32bits
    sd a,i(b) #from register a into memory b[i]
    sw c,i(d)
    # i must be an immediate!
  • In RISC-V: Memory Accessed only by data transfer instructions.
  • Byte addressed: Each address identifies an 8-bit byte.
    按照byte(8bits)寻址,即对于一个6464bits长的地址,可以寻址到2643=2612^{64-3} = 2^{61}个地址。
  • Little Endian: Least-significant byte at least address of a word.
  • Memory Alignment ? NOT REQUIRED!

Endianness/byte order

  • Big endian:数据的高字节存放在低地址,低字节存放在高地址.e.g PowerPC
  • Little endian: 数据的高字节存放在高地址,低字节存放在低地址. e.g RISC-V

e.g 32位机器上存放0x123456789:

处理时一般从低地址开始处理。大端方便人阅读;小端方便机器处理,也是机器架构的主流。

  • Registers are faster to access than memory.
  • Operating on memory data requires loads and stores
  • Compiler must use registers for variables as much as possible.
    • Spilling registers: Putting less commonly used variables(or those needed later) into memory
    • Register optimization

Constant or Immediate Operand

1
addi a,b,i # a <- b + i

signed and unsigned numbers

Bits are just bits (no inherent meaning),but there are conventions define relationship between bits and numbers.

  • Unsigned: x=xn2n1++x121+x020[0,2n1]Zx = x_n2^{n - 1} + \cdots + x_12^1 + x_02^0 \in [0,2^n - 1]\cap Z

  • 2’s-Complement Signed: x=xn2n1++x121+x020[2n1,2n11]Zx = -x_n2^{n - 1} + \cdots + x_12^1 + x_02^0 \in [-2^{n-1},2^{n - 1}-1]\cap Z

  • Sign Extension: Replicate the sign bit to the left; extend with 0s for unsigned values.

    1
    2
    lb a,i(b)
    lbu a,i(b)

Representing Instructions in the Computer

Stored-program

  • Instructions are represented as numbers

  • Programs can be stored in memory like numbers

  • Instructions represented in binary, just like data

  • Instructions and data stored in memory

  • Programs can operate on programs e.g compilers, linkers, …

  • Binary compatibility allows compiled programs to work on different computers, Standardized ISAs

Logical Operation

Instructions for Making Decisions

Supporting Procedures in Computer Hardware

Procedure/function is be used to structure programs.

  • A stored subroutine that performs a specific task based on the parameters with which it is provided
  • easier to understand, allow code to be reused

Steps

  1. Place Parameters in a place where the procedure can access them (in registers x10~x17)
  2. Transfer control to the procedure
  3. Acquire the storage resources needed for the procedure
  4. Perform the desired task
  5. Place the result value in a place where the calling program can access it
  6. Return control to the point of origin (address in x1)

Instructions

1
jal x1,Label # x1 <- pc + 4 and jump to Label
1
jalr x0,0(x1) #jump to 0 + x1(ra)

Using More Registers

Stack:Ideal data structure for spilling registers. For saved registers, the callee saves in satck and restores them back.

  • Stack pointer sp
  • grow from higher address to lower address.
    • Push: sp= sp-8
    • Pop: sp = sp+8
1
2
3
4
5
6
7
8
9
10
addi sp,sp,-24 
sd x5,16(sp)
sd x6,8(sp)
sd x20,0(sp)
...
ld x20,0(sp)
ld x6,8(sp)
ld x5,16(sp)
addi sp,sp,24
jalr x0,0(x1)

Communicating with People

MIPS Addressing for 32 Bit Immediates and Addresses

Translanting and starting a Program

A C Sort Example to Put It All together

Arrays Versus Pointers

Real Stuff: IA-32 Instructions

Fallacies and Pitfalls

Concluding Remarks

Historical Perspective and Further Reading


CO C2 Instructions
http://example.com/2023/04/18/CO-2/
Author
Tekhne Chen
Posted on
April 18, 2023
Licensed under