Risk Warning: Beware of illegal fundraising in the name of 'virtual currency' and 'blockchain'. — Five departments including the Banking and Insurance Regulatory Commission
Information
Discover
Search
Login
简中
繁中
English
日本語
한국어
ภาษาไทย
Tiếng Việt
BTC
ETH
HTX
SOL
BNB
View Market
Explain the Cairo command in detail
Sin7y
特邀专栏作者
2022-09-28 02:44
This article is about 2540 words, reading the full article takes about 4 minutes
This article interprets Cairo instructions in detail from multiple perspectives, involving state transitions, transition logic, instruction verification, instruction examples, etc.

first level title

1. Instruction structure"12345678") then the instruction occupies 2 words, and the value is stored in the second word. The first word of each directive consists of the following elements:

first level title

2. State transition

The state transition function represents a general state transition unit (since it contains the processing logic of all instruction types), and a calculation is usually decomposed into multiple sequentially executed instructions, so we need:

b. Make sure the executed command is a valid command

secondary title

If the state before and after the execution of the instruction is consistent, then its state update must be performed according to the following logic:

secondary title

As shown in Figure 1, a command consists of the following elements:

3. Command example

secondary title

3.1 Asserting equality

< left_handle_op > = < right_handle_op >

The assert equality instruction can be expressed with the following syntax:

Note2:It ensures that both sides of the formula are equal, otherwise program execution will be returned.

Division and subtraction can be expressed as multiplication and addition, respectively, with different operand orders.

Figure 4 gives some examples of assert equality instructions, and the corresponding flag values ​​for each instruction:

explainexplain

instruction[fp+1] = 5:

◦ next_ap = ap => ap_update = 00 = 0 

◦ next_pc = pc + instruction_size => pc_update = 000 = 0 

◦ for assert instruction => opcode = 4

◦ op0 and op1 do not have add or mul => res_logic(res) = 00 = 0

◦ Immediate data present => op1_src(op1) = 001 = 1

◦ Immediate address instruction address adjacent => off_op1 = 1

◦ Left side of equation [fp + 1] => dst_reg(dst) = 1

◦ op0_reg/ off_op0 =>inital value(1/-1) //Because this instruction does not use these flags, fill in the default value

secondary title

3.2 Conditional and unconditional jumps

The jmp instruction allows changing the value of the program counter pc.

Cairo supports relative jumps (the operands represent offsets relative to the current pc) and absolute jumps - represented by the keywords rel and abs respectively; the jmp instruction may be conditional, such as when the value of a memory unit is not 0 , trigger the jmp instruction.

# Unconditional jumps. 

jmp abs

jmp rel 

# Conditional jumps. 

jmp rel if

Figure 5 gives some examples of jmp instructions, and the corresponding flag values ​​for each instruction:

explainexplain

Instruction jmp rel [ap +1] + [fp - 7]:

◦ next_ap = ap => ap_update = b00 = 0 

◦ next_pc = pc + res=> pc_update = b010 = 2 

◦ res = op0 + op1 => res_logic(res) = b01 = 1

◦ op1: [fp - 7] => op1_src(op1) = b010 = 2 

◦ op1: [fp - 7] => off_op1 = -7 

◦ op0: [ap + 1] => op0_src(op0) = 0 

◦ op0: [ap + 1] => off_op0 = 1 

◦ dst_reg/ off_dst =>inital value(1/-1) ///Because this instruction does not use these flags, fill in the default value

secondary title

3.3 call and return

The call and ret instructions allow the implementation of a function stack. The call instruction updates the program counter (pc) and frame pointer (fp) registers. The update of the program counter is similar to the jmp instruction. The previous value of fp is written to [ap] to allow the ret instruction to reset the value of fp to what it was before the call; similarly, the returned pc (the address of the instruction following the call instruction) is written to [ap+1], to allow the ret instruction to jump back and continue execution of the code following the calling instruction. Since two memory locations were written, ap is advanced by 2 and fp is set to the new ap.

call ret

call rel  

ret

Figure 6 gives some examples of call and ret instructions, and the corresponding flag values ​​for each instruction:

explainexplain

Instruction call abs [fp + 4]:

◦ next_ap = ap => ap_update = b00 = 0 

◦ next_pc = res => pc_update = b001 = 1 

◦ res = op1 => res_logic(res) = b00 = 0 

◦ op1: [fp + 4] => op1_src(op1) = b010 = 2 

◦ op1: [fp + 4] => off_op1 = 4 

◦ op0_reg/ off_op0 =>◦ For call instruction => opcode = 0

◦ dst_reg/ off_dst =>inital value(0/0) ///Because this instruction does not use these flags, fill in the default value

secondary title

3.4 Advanced APs< op >Command ap + =

Figure 7 gives some illustrations of the high-level ap commands, and the flags corresponding to each command:

explainexplain

Instruction ap += 123:

◦ next_ap = ap + res => ap_update = b01 = 1

◦ next_pc = pc + instruction_size => pc_update = b000 = 0 

◦ res = op1 => res_logic(res) = b00 = 0 

◦ op1 = 123 => op1_src(op1) = b001 = 1 

◦ op1 = 123 => off_op1 = 1 

◦ op0_reg/ off_op0 =>◦ For advancing ap command => opcode = 0

◦ dst_reg/ off_dst =>Sin7y was established in 2021 and is composed of top blockchain developers. We are both a project incubator and a blockchain technology research team, exploring the most important and cutting-edge technologies such as EVM, Layer2, cross-chain, privacy computing, and autonomous payment solutions.

about Us

Sin7y was established in 2021 and is composed of top blockchain developers. We are both a project incubator and a blockchain technology research team, exploring the most important and cutting-edge technologies such as EVM, Layer2, cross-chain, privacy computing, and autonomous payment solutions.

WeChat public account: Sin7Y

GitHub | Twitter | Telegram | MediumMirror | HackMD | HackerNoon

Developer
Welcome to Join Odaily Official Community