Вы находитесь на странице: 1из 19

Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH

Bộ môn Máy tính – Hệ thống nhúng

Phụ lục - CÁC LỆNH ASSEMBLY CỦA CPU NIOS II

A B C D E F I J L M N O R S T W X
1 add beq call div eret flushd initd jmp ldb/ ldbio mov nextpc or rdctl sll trap wrctl xor
2 addi bge callr divu flushda initda jmpi ldbu/ ldbuio movhi nop orhi rdrps slli wrprs xorhi
3 and bgeu cmpeq flushi initi ldh/ ldhio movi nor ori ret sra xori
4 andhi bgt cmpeqi flushp ldhu/ ldhuio movia rol srai
5 andi bgtu cmpge ldw/ ldwio movui roli srl
6 ble cmpgei mul ror srli
7 bleu cmpgeu muli stb/ stbio
8 blt cmpgeui mulxss sth/ sthio
9 bltu cmpgt mulxsu stw/ stwio
10 bne cmpgti mulxuu sub
11 br cmpgtu subi
12 break cmpgtui sync
13 bret cmple
14 cmplei
15 cmpleu
16 cmpleui
17 cmplt
18 cmplti
19 cmpltu
20 cmpltui
21 cmpne
22 cmpnei
23 custom

62
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

o Nội dung các ghi chú được sử dụng trong bảng liệt kê các lệnh dưới đây
Ghi chú Ý nghĩa
X Y X is written with Y
The program counter (PC) is written with address X; the instruction at X
PC X
is the next instruction to execute
PC The address of the assembly instruction in question
rA, rB, rC One of the 32-bit general-purpose registers
IMMn An n-bit immediate value, embedded in the instruction word
IMMED An immediate value
Xn The nth bit of X, where n = 0 is the LSB
Xn..m Consecutive bits n through m of X
0xNNMM Hexadecimal notation
Bitwise concatenation
X:Y
For example, (0x12 : 0x34) = 0x1234
The value of X after being sign-extended into a full register-
𝛿(𝑥)
sized signed integer
X >> n The value X after being right-shifted n bit positions
X << n The value X after being left-shifted n bit positions
X&Y Bitwise logical AND
X|Y Bitwise logical OR
X^Y Bitwise logical XOR
~X Bitwise logical NOT (one’s complement)
Mem8[X] The byte located in data memory at byte-address X
Mem16[X] The halfword located in data memory at byte-address X
Mem32[X] The word located in data memory at byte-address X
Label An address label specified in the assembly file
(signed) rX The value of rX treated as a signed number
(unsigned) rX The value of rX, treated as an unsigned number

o Chi tiết các lệnh được liệt kê như sau


addi (A2)
add (A1)
add immediate
Description: Calculates the sum of rA and rB. Stores the Description: Sign-extends the 16-bit immediate value and
result in rC. Used for both signed and unsigned addition. adds it to the value of rA. Stores the sum in rB.
Usage: Carry Detection (unsigned operands) and Overflow Usage: Carry Detection (unsigned operands) and Overflow
Detection (signed operands) Detection (signed operands)
Operation: rC ← rA + rB Operation: rB ← rA + σ (IMM16)

62
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

Assembler Syntax: add rC, rA, rB Assembler Syntax: addi rB, rA, IMM16
Example: add r6, r7, r8 Example: addi r6, r7, -100
Exceptions: None Exceptions: None
Instruction Type: R Instruction Type: I
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
C = Register index of operand rC IMM16 = 16-bit signed immediate value
and (A3) andhi (A4)
bitwise logical and bitwise logical and immediate into high halfword
Description: Calculates the bitwise logical AND of rA and Description: Calculates the bitwise logical AND of rA and
rB and stores the result in rC (IMM16 : 0x0000) and stores the result in rB.
Operation: rC ← rA & rB Operation: rB ← rA & (IMM16 : 0x0000)
Assembler Syntax: and rC, rA, rB Assembler Syntax: andhi rB, rA, IMM16
Example: and r6, r7, r8 Example: andhi r6, r7, 100
Exceptions: None Exceptions: None
Instruction Type: R Instruction Type: I
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
C = Register index of operand rC IMM16 = 16-bit unsigned immediate value
andi (A5) beq (A6)
bitwise logical and immediate branch if equal
Description: Calculates the bitwise logical AND of rA and Description: If rA == rB, then beq transfers program
(0x0000 : IMM16) and stores the result in rB. control to the instruction at label. In the instruction
Operation: rB ← rA & (0x0000 : IMM16) encoding, the offset given by IMM16 is treated as a signed
Assembler Syntax: andi rB, rA, IMM16 number of bytes relative to the instruction immediately
Example: andi r6, r7, 100 following beq. The two least-significant bits of IMM16 are
Exceptions: None always zero, because instruction addresses must be word-
Instruction Type: I aligned.
Instruction Fields: A = Register index of operand rA Operation: if (rA == rB)
B = Register index of operand rB then PC ← PC + 4 + σ (IMM16)
IMM16 = 16-bit unsigned immediate value else PC ← PC + 4
Assembler Syntax: beq rA, rB, label
Example: beq r6, r7, label
Exceptions: Misaligned destination address
Instruction Type: I
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
bge (A7) bgeu (A8)
branch if greater than or equal signed branch if greater than or equal unsigned
Description: If (signed)rA>=(signed)rB, then bge transfers Description: If (unsigned)rA>=(unsigned)rB, then bgeu
program control to the instruction at label. In the transfers program control to the instruction at label. In the
instruction encoding, the offset given by IMM16 is treated instruction encoding, the offset given by IMM16 is treated
as a signed number of bytes relative to the instruction as a signed number of bytes relative to the instruction
immediately following bge. The two least-significant bits immediately following bgeu. The two least-significant bits
of IMM16 are always zero, because instruction addresses of IMM16 are always zero, because instruction addresses
must be word-aligned. must be word-aligned.
Operation: if ((signed)rA >= (signed) rB) Operation: if ((unsigned)rA >= (unsigned)rB)
then PC← PC+4+ σ (IMM16) then PC←PC+4 + σ (IMM16)
else PC ← PC+4 else PC ←PC+4
Assembler Syntax: bge rA, rB, label Assembler Syntax: bgeurA,rB,label
Example: bge r6, r7, top_of_loop Example: bgeur6,r7,top_of_loop
Exceptions: Misaligned destination address Exceptions: Misaligned destination address
Instruction Type: I Instruction Type: I
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB

63
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

IMM16 = 16-bit signed immediate value IMM16 = 16-bit signed immediate value
bgt (A9) bgtu (A10)
branch if greater than signed branch if greater than unsigned
Description: If (signed)rA>(signed)rB, then bgt transfers Description: If (unsigned)rA>(unsigned)rB, then bgtu
program control to the instruction at label. transfers program control to the instruction at label.
Pseudo-instruction: bgt is implemented with the blt Pseudo-instruction: bgtu is implemented with the bltu
instruction by swapping the register operands instruction by swapping the register operands.
Operation: if ((signed)rA>(signed)rB) Operation: if ((unsigned)rA>(unsigned)rB)
then PC ← label then PC ← label
else PC ← PC+4 else PC ← PC+4
Assembler Syntax: bgt rA, rB, label Assembler Syntax: bgtu rA, rB, label
Example: bgt r6, r7, top_of_loop Example: bgtu r6, r7, top_of_loop
ble (A11) bleu (A12)
branch if less than or equal signed branch if less than or equal to unsigned
Description: If (signed)rA<=(signed)rB, then ble transfers Description: If (unsigned)rA<=(unsigned)rB, then bleu
program control to the instruction at label. transfers program counter to the instruction at label.
Pseudo-instruction: ble is implemented with the bge Pseudo-instruction: bleu is implemented with the bgeu
instruction by swapping the register operands. instruction by swapping the register operands.
Operation: if ((signed)rA<=(signed)rB) Operation: if ((unsigned)rA<=(unsigned) rB)
then PC ← label then PC ← label
else PC ← PC+4 else PC ← PC+4
Assembler Syntax: ble rA, rB, label Assembler Syntax: bleu rA, rB, label
Example: ble r6, r7, top_of_loop Example: bleu r6, r7, top_of_loop
blt (A13) bltu (A14)
branch if less than signed branch if less than unsigned
Description: If (signed)rA<(signed)rB, then blt transfers Description: If (unsigned)rA<(unsigned)rB, then bltu
program control to the instruction at label. In the transfers program control to the instruction at label. In the
instruction encoding, the offset given by IMM16 is treated instruction encoding, the offset given by IMM16 is treated
as a signed number of bytes relative to the instruction as a signed number of bytes relative to the instruction
immediately following blt. The two least-significant bits of immediately following bltu. The two least-significant bits
IMM16 are always zero, because instruction addresses of IMM16 are always zero, because instruction addresses
must be word-aligned. must be word-aligned.
Operation: if ((signed)rA<(signed)rB) Operation: if ((unsigned)rA<(unsigned)rB)
then PC ←PC+4+ σ (IMM16) then PC ← PC+4+ σ (IMM16)
else PC ←PC+4 else PC ← PC+4
Assembler Syntax: blt rA, rB, label Assembler Syntax: bltu rA, rB, label
Example: blt r6, r7, top_of_loop Example: bltu r6, r7, top_of_loop
Exceptions: Misaligned destination address Exceptions: Misaligned destination address
Instruction Type: I Instruction Type: I
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
IMM16 = 16-bit signed immediate value IMM16 = 16-bit signed immediate value
bne (A15) br (A16)
branch if not equal unconditional branch
Description: If rA != rB, then bne transfers program Description: Transfers program control to the instruction at
control to the instruction at label. In the instruction label. In the instruction encoding, the offset given by
encoding, the offset given by IMM16 is treated as a signed IMM16 is treated as a signed number of bytes relative to
number of bytes relative to the instruction immediately the instruction immediately following br. The two least-
following bne. The two least-significant bits of IMM16 are significant bits of IMM16 are always zero, because
always zero, because instruction addresses must be word- instruction addresses must be word-aligned.
aligned. Operation: PC ← PC+4+ σ (IMM16)
Operation: if (rA != rB) Assembler Syntax: br label
then PC ← PC+4+ σ (IMM16) Example: br top_of_loop
else PC ← PC+4 Exceptions: Misaligned destination address
Assembler Syntax: bne rA, rB, label Instruction Type: I
Example: bne r6, r7,top_of_loop Instruction Fields:

64
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

Exceptions: Misaligned destination address IMM16 = 16-bit signed immediate value


Instruction Type: I
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
break (B1) bret (B2)
debugging breakpoint breakpoint return
Description: Breaks program execution and transfers Description: Copies the value of bstatus to the status
control to the debugger break-processing routine. Saves the register, then transfers execution to the address in ba.
address of the next instruction in register ba and saves the Usage: bret is used by debuggers exclusively and should
contents of the status register in bstatus. Disables not appear in user programs, operating systems, or
interrupts, then transfers execution to the break handler. exception handlers.
The 5-bit immediate field imm5 is ignored by the Operation: status ← bstatus
processor, but it can be used by the debugger. PC ← ba
break with no argument is the same as break 0. Assembler Syntax: bret
Usage: break is used by debuggers exclusively. Only Example: bret
debuggers should place break in a user program, operating Exceptions: Misaligned destination address
system, or exception handler. The address of the break Supervisor-only instruction
handler is specified at system generation time. Instruction Type: R
Some debuggers support break and break 0 instructions in Instruction Fields: None
source code. These debuggers treat the break instruction as
a normal breakpoint.
Operation: bstatus ←status
PIE ←0
U ←0
ba ←PC+4
PC ←break handler address
Assembler Syntax: break
break imm5
Example: break
Exceptions: Break
Instruction Type: R
Instruction Fields: IMM5 = Type of breakpoint
call (B3) callr (B4)
call subroutine call subroutine in register
Description: Saves the address of the next instruction in Description: Saves the address of the next instruction in
register ra, and transfers execution to the instruction at the return address register, and transfers execution to the
address (PC31..28 : IMM26 × 4). address contained in register rA.
Usage: call can transfer execution anywhere within the Usage: callr is used to dereference C-language function
256-megabyte (MB) range determined by PC31..28. The pointers.
NiosII GNU linker does not automatically handle cases in Operation: ra ←PC + 4
which the address is out of this range. PC ←rA
Operation: ra ← PC + 4 Assembler Syntax: callr rA
PC ← (PC31..28 : IMM26 × 4) Example: callr r6
Assembler Syntax: call label Exceptions: Misaligned destination address
Example: call write_char Instruction Type: R
Exceptions: None Instruction Fields: A = Register index of operand rA
Instruction Type: J
Instruction Fields:
IMM26 = 26-bit unsigned immediate value
cmpeq (B5) cmpeqi (B6)
compare equal compare equal immediate
Description: If rA == rB, then stores 1 to rC; otherwise, Description: Sign-extends the 16-bit immediate value
stores 0 to rC. IMM16 to 32 bits and compares it to the value of rA. If
Usage: cmpeq performs the == operation of the C rA == σ (IMM16), cmpeqi stores 1 to rB; otherwise stores
programming language. Also, cmpeq can be used to 0 to rB.

65
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

implement the C logical negation operator “!”. Usage: cmpeqi performs the == operation of the C
Operation: if (rA == rB) programming language
then rC ←1 Operation: if (rA σ (IMM16))
else rC ←0 then rB ←1
Assembler Syntax: cmpeq rC, rA, rB else rB ←0
Example: cmpeq r6, r7, r8 Assembler Syntax: cmpeqi rB, rA, IMM16
Exceptions: None Example: cmpeqi r6, r7, 100
Instruction Type: R Exceptions: None
Instruction Fields: A = Register index of operand rA Instruction Type: I
B = Register index of operand rB Instruction Fields: A = Register index of operand rA
C = Register index of operand rC B = Register index of operand rB
IMM16 = 16-bit signed immediate value
cmpge (B7) cmpgei (B8)
compare greater than or equal signed compare greater than or equal signed immediate
Description: If rA >= rB, then stores 1 to rC; otherwise Description: Sign-extends the 16-bit immediate value
stores 0 to rC. IMM16 to 32 bits and compares it to the value of rA. If
Usage: cmpge performs the signed >= operation of the C rA >= σ (IMM16), then cmpgei stores 1 to rB; otherwise
programming language. stores 0 to rB.
Operation: if ((signed) rA >= (signed) rB) Usage: cmpgei performs the signed >= operation of the C
then rC ←1 programming language.
else rC ←0 Operation: if ((signed) rA >= (signed) σ (IMM16))
Assembler Syntax: cmpge rC, rA, rB then rB←1
Example: cmpge r6, r7, r8 else rB ←0
Exceptions: None Assembler Syntax: cmpgei rB, rA, IMM16
Instruction Type: I Example: cmpgei r6, r7, 100
Instruction Fields: A = Register index of operand rA Exceptions: None
B = Register index of operand rB Instruction Type: R
C = Register index of operand rC Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
cmpgeu (B9) cmpgeui (B10)
compare greater than or equal unsigned compare greater than or equal unsigned immediate
Description: If rA >= rB, then stores 1 to rC; otherwise Description: Zero-extends the 16-bit immediate value
stores 0 to rC. IMM16 to 32 bits and compares it to the value of rA. If
Usage: cmpgeu performs the unsigned >= operation of the rA >= (0x0000 : IMM16), then cmpgeui stores 1 to rB;
C programming language. otherwise stores 0 to rB.
Operation: if ((unsigned) rA >= (unsigned) rB) Usage: cmpgeui performs the unsigned >= operation of the
then rC ←1 C programming language.
else rC ←0 Operation:
Assembler Syntax: cmpgeu rC, rA, rB if ((unsigned) rA >= (unsigned) (0x0000 : IMM16))
Example: cmpgeu r6, r7, r8 then rB ←1
Exceptions: None else rB ←0
Instruction Type: R Assembler Syntax: cmpgeui rB, rA, IMM16
Instruction Fields: A = Register index of operand rA Example: cmpgeui r6, r7, 100
B = Register index of operand rB Exceptions: None
C = Register index of operand rC Instruction Type: I
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit unsigned immediate value
cmpgt (B11) cmpgti (B12)
compare greater than signed compare greater than signed immediate
Description: If rA > rB, then stores 1 to rC; otherwise Description: Sign-extends the immediate value IMMED to
stores 0 to rC. 32 bits and compares it to the value of rA. If rA >
Usage: cmpgt performs the signed > operation of the C σ (IMMED), then cmpgti stores 1 to rB; otherwise stores 0
programming language. to rB.
Operation: if ((signed) rA > (signed) rB) Usage: cmpgti performs the signed > operation of the C

66
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

then rC ←1 programming language. The maximum allowed value of


else rC ←0 IMMED is 32766. The minimum allowed value is –32769.
Assembler Syntax: cmpgt rC, rA, rB Operation: if ((signed) rA > (signed) IMMED)
Example: cmpgt r6, r7, r8 then rB ←1
Pseudo-instruction: cmpgt is implemented with the cmplt else rB ←0
instruction by swapping its rA and rB operands. Assembler Syntax: cmpgti rB, rA, IMMED
Example: cmpgti r6, r7, 100
Pseudo-instruction: cmpgti is implemented using a
cmpgei instruction with an IMM16 immediate value of
IMMED + 1.
cmpgtu (B13) cmpgtui (B14)
compare greater than unsigned compare greater than unsigned immediate
Description: If rA > rB, then stores 1 to rC; otherwise Description: Zero-extends the immediate value IMMED to
stores 0 to rC. 32 bits and compares it to the value of rA. If rA >
Usage: cmpgtu performs the unsigned > operation of the C IMMED, then cmpgtui stores 1 to rB; otherwise stores 0 to
programming language. rB.
Operation: if ((unsigned) rA > (unsigned) rB) Usage: cmpgtui performs the unsigned > operation of the C
then rC ←1 programming language. The maximum allowed value of
else rC ←0 IMMED is 65534. The minimum allowed value is 0.
Assembler Syntax: cmpgtu rC, rA, rB Operation: if ((unsigned) rA > (unsigned) IMMED)
Example: cmpgtu r6, r7, r8 then rB ← 1
Pseudo-instruction: cmpgtu is implemented with the else rB ←0
cmpltu instruction by swapping its rA and rB operands. Assembler Syntax: cmpgtui rB, rA, IMMED
Example: cmpgtui r6, r7, 100
Pseudo-instruction: cmpgtui is implemented using a
cmpgeui instruction with an IMM16 immediate value of
IMMED + 1.
cmple (B15) cmplei (B16)
compare less than or equal signed compare less than or equal signed immediate
Description: If rA <= rB, then stores 1 to rC; otherwise Description: Sign-extends the immediate value IMMED to
stores 0 to rC. 32 bits and compares it to the value of rA. If rA <=
Usage: cmple performs the signed <= operation of the C σ (IMMED), then cmplei stores 1 to rB; otherwise stores 0
programming language. to rB.
Operation: if ((signed) rA <= (signed) rB) Usage: cmplei performs the signed <= operation of the C
then rC ←1 programming language. The maximum allowed value of
else rC ←0 IMMED is 32766. The minimum allowed value is –32769.
Assembler Syntax: cmple rC, rA, rB Operation: if ((signed) rA < (signed) IMMED)
Example: cmple r6, r7, r8 then rB ←1
Pseudo-instruction: cmple is implemented with the cmpge else rB ←0
instruction by swapping its rA and rB operands. Assembler Syntax: cmplei rB, rA, IMMED
Example: cmplei r6, r7, 100
Pseudo-instruction: cmplei is implemented using a cmplti
instruction with an IMM16 immediate value of
IMMED + 1.
cmpleu (C1) cmpleui (C2)
compare less than or equal unsigned compare less than or equal unsigned immediate
Description: If rA <= rB, then stores 1 to rC; otherwise Description: Zero-extends the immediate value IMMED to
stores 0 to rC. 32 bits and compares it to the value of rA. If rA < IMMED,
Usage: cmpleu performs the unsigned <= operation of the then cmpleui stores 1 to rB; otherwise stores 0 to rB.
C programming language. Usage:
Operation: if ((unsigned) rA < (unsigned) rB) cmpleui performs the unsigned <= operation of the C
then rC ←1 programming language. The maximum allowed value of
else rC ←0 IMMED is 65534. The minimum allowed value is 0.
Assembler Syntax: cmpleu rC, rA, rB Operation: if ((unsigned) rA <= (unsigned) IMMED)
Example: cmpleu r6, r7, r8 then rB ←1
Pseudo-instruction: cmpleu is implemented with the else rB ←0

67
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

cmpgeu instruction by swapping its rA and rB operands. Assembler Syntax: cmpleui rB, rA, IMMED
Example: cmpleui r6, r7, 100
Pseudo-instruction: cmpleui is implemented using a
cmpltui instruction with an IMM16 immediate value of
IMMED + 1.
cmplt (C3) cmplti (C4)
compare less than signed compare less than signed immediate
Description: If rA < rB, then stores 1 to rC; otherwise Description: Sign-extends the 16-bit immediate value
stores 0 to rC. IMM16 to 32 bits and compares it to the value of rA. If
Usage: cmplt performs the signed < operation of the C rA < σ (IMM16), then cmplti stores 1 to rB; otherwise
programming language. stores 0 to rB.
Operation: if ((signed) rA < (signed) rB) Usage: cmplti performs the signed < operation of the C
then rC ←1 programming language.
else rC ←0 Operation: if ((signed) rA < (signed) σ (IMM16))
Assembler Syntax: cmplt rC, rA, rB then rB ←1
Example: cmplt r6, r7, r8 else rB ←0
Exceptions: None Assembler Syntax: cmplti rB, rA, IMM16
Instruction Type: R Example: cmplti r6, r7, 100
Instruction Fields: A = Register index of operand rA Exceptions: None
B = Register index of operand rB Instruction Type: I
C = Register index of operand rC Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
cmpltu (C5) cmpltui (C6)
compare less than unsigned compare less than unsigned immediate
Description: If rA < rB, then stores 1 to rC; otherwise Description: Zero-extends the 16-bit immediate value
stores 0 to rC. IMM16 to 32 bits and compares it to the value of rA. If
Usage: cmpltu performs the unsigned < operation of the C rA < (0x0000 : IMM16), then cmpltui stores 1 to rB;
programming language. otherwise stores 0 to rB.
Operation: if ((unsigned) rA < (unsigned) rB) Usage: cmpltui performs the unsigned < operation of the C
then rC ←1 programming language.
else rC ←0 Operation:
Assembler Syntax: cmpltu rC, rA, rB if ((unsigned) rA < (unsigned) (0x0000 : IMM16))
Example: cmpltu r6, r7, r8 then rB ←1
Exceptions: None else rB ←0
Instruction Type: R Assembler Syntax: cmpltui rB, rA, IMM16
Instruction Fields: A = Register index of operand rA Example: cmpltui r6, r7, 100
B = Register index of operand rB Exceptions: None
C = Register index of operand rC Instruction Type: I
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit unsigned immediate value
cmpne (C7) cmpnei (C8)
compare not equal compare not equal immediate
Description: If rA != rB, then stores 1 to rC; otherwise Description: Sign-extends the 16-bit immediate value
stores 0 to rC. IMM16 to 32 bits and compares it to the value of rA. If
Usage: cmpne performs the != operation of the C rA != σ (IMM16), then cmpnei stores 1 to rB; otherwise
programming language. stores 0 to rB.
Operation: if (rA != rB) Usage: cmpnei performs the != operation of the C
then rC ←1 programming language.
else rC ←0 Operation: if (rA != σ (IMM16))
Assembler Syntax: cmpne rC, rA, rB then rB←1
Example: cmpne r6, r7, r8 else rB ←0
Exceptions: None Assembler Syntax: cmpnei rB, rA, IMM16
Instruction Type: R Example: cmpnei r6, r7, 100
Instruction Fields: A = Register index of operand rA Exceptions: None

68
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

B = Register index of operand rB Instruction Type: I


C = Register index of operand rC Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
custom (C9) div (C10)
custom instruction divide
Description: The custom opcode provides access to up to Description: Treating rA and rB as signed integers, this
256 custom instructions allowed by the NiosII architecture. instruction divides rA by rB and then stores the integer
The function implemented by a custom instruction is user- portion of the resulting quotient to rC. After attempted
defined and is specified at system generation time. The 8- division by zero, the value of rC is undefined. There is no
bit immediate N field specifies which custom instruction to divide-by-zero exception. After dividing –2147483648 by –
use. Custom instructions can use up to two parameters, xA 1, the value of rC is undefined (the number +2147483648 is
and xB, and can optionally write the result to a register xC. not representable in 32 bits). There is no overflow
Usage: To access a custom register inside the custom exception.
instruction logic, clear the bit readra, readrb, or writerc that NiosII processors that do not implement the div instruction
corresponds to the register field. In assembler syntax, the cause an unimplemented instruction exception.
notation cN refers to register N in the custom register file Usage: Remainder of Division
and causes the assembler to clear the c bit of the opcode. Operation: rC ←rA ÷ rB
For example, custom0,c3,r5,r0 performs custom instruction Assembler Syntax: div rC, rA, rB
0, operating on general-purpose registers r5 and r0, and Example: div r6, r7, r8
stores the result in custom register 3. Exceptions: Division error
Operation: if c == 1 Unimplemented instruction
then rC ← fN(rA, rB, A, B, C) Instruction Type: R
else Ø ← fN(rA, rB, A, B, C) Instruction Fields: A = Register index of operand rA
Assembler Syntax: custom N, xC, xA, xB B = Register index of operand rB
Where xA means either general purpose register rA, or C = Register index of operand rC
custom register cA.
Example: custom 0,c6,r7,r8
Exceptions: None
Instruction Type: R
Instruction Fields: A = Register index of operand A
B = Register index of operand B
C = Register index of operand C
readra = 1 if instruction uses rA, 0 otherwise
readrb = 1 if instruction uses rB, 0 otherwise
writerc = 1 if instruction provides result for rC, 0 otherwise
N = 8-bit number that selects instruction
divu (C11) eret (C12)
divide unsigned exception return
Description: Treating rA and rB as unsigned integers, this Description: Copies the value of estatus into the status
instruction divides rA by rB and then stores the integer register, and transfers execution to the address in ea.
portion of the resulting quotient to rC. After attempted Usage: Use eret to return from traps, external interrupts,
division by zero, the value of rC is undefined. There is no and other exception handling routines.
divide-by-zero exception. Note that before returning from hardware interrupt
NiosII processors that do not implement the divu exceptions, the exception handler must adjust the ea
instruction cause an unimplemented instruction exception. register.
Usage: Remainder of Division Operation: status ← estatus
Operation: rC ←rA ÷ rB PC ← ea
Assembler Syntax: divu rC, rA, rB Assembler Syntax: eret
Example: divu r6, r7, r8 Example: eret
Exceptions: Division error Exceptions: Misaligned destination address
Unimplemented instruction Supervisor-only instruction
Instruction Type: R Instruction Type: R
Instruction Fields: A = Register index of operand rA Instruction Fields: None
B = Register index of operand rB
C = Register index of operand rC
flushd (C13) flushda (C14)

69
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

flush data cache line flush data cache address


Description: If the NiosII processor implements a direct Description: If the NiosII processor implements a direct
mapped data cache, flushd writes the data cache line that is mapped data cache, flushda writes the data cache line that
mapped to the specified address back to memory if the line is mapped to the specified address back to memory if the
is dirty, and then clears the data cache line. Unlike flushda, line is dirty, and then clears the data cache line. Unlike
flushd writes the dirty data back to memory even when the flushd, flushda writes the dirty data back to memory only
addressed data is not currently in the cache. This process when the addressed data is currently in the cache. This
comprises the following steps: process comprises the following steps:
■ Compute the effective address specified by the sum of rA ■ Compute the effective address specified by the sum of rA
and the signed 16-bit immediate value. and the signed 16-bit immediate value.
■ Identify the data cache line associated with the computed ■ Identify the data cache line associated with the computed
effective address. Each data cache effective address effective address. Each data cache effective address
comprises a tag field and a line field. When identifying the comprises a tag field and a line field. When identifying the
data cache line, flushd ignores the tag field and only uses line, flushda uses both the tag field and the line field.
the line field to select the data cache line to clear. ■ Compare the cache line tag with the effective address to
■ Skip comparing the cache line tag with the effective determine if the addressed data is currently cached. If the
address to determine if the addressed data is currently tag fields do not match, the effective address is not
cached. Because flushd ignores the cache line tag, flushd currently cached, so the instruction does nothing.
flushes the cache line regardless of whether the specified ■ If the data cache line is dirty and the tag fields match,
data location is currently cached. write the dirty cache line back to memory. A cache line is
■ If the data cache line is dirty, write the line back to dirty when one or more words of the cache line have been
memory. A cache line is dirty when one or more words of modified by the processor, but are not yet written to
the cache line have been modified by the processor, but are memory.
not yet written to memory. ■ Clear the valid bit for the line.
■ Clear the valid bit for the line. If the NiosII processor core does not have a data cache, the
If the NiosII processor core does not have a data cache, the flushda instruction performs no operation.
flushd instruction performs no operation. Usage: Use flushda to write dirty lines back to memory
Usage: Use flushd to write dirty lines back to memory even only if the addressed memory location is currently in the
if the addressed memory location is not in the cache, and cache, and then flush the cache line.
then flush the cache line. Operation: Flushes the data cache line currently caching
Operation: Flushes the data cache line associated with address rA+ σ (IMM16)
address rA+ σ (IMM16). Assembler Syntax: flushda IMM16(rA)
Assembler Syntax: flushd IMM16(rA) Example: flushda -100(r6)
Example: flushd -100(r6) Exceptions: Supervisor-only data address
Exceptions: None Fast TLB miss (data)
Instruction Type: I Double TLB miss (data)
Instruction Fields: A = Register index of operand rA MPU region violation (data)
IMM16 = 16-bit signed immediate value Instruction Type: I
Instruction Fields: A = Register index of operand rA
IMM16 = 16-bit signed immediate value
flushi (C15) flushp (C16)
flush instruction cache line flush pipeline
Description: Ignoring the tag, flushi identifies the Description: Ensures that any instructions prefetched after
instruction cache line associated with the byte address in the flushp instruction are removed from the pipeline.
rA, and invalidates that line. If the NiosII processor core Usage: Use flushp before transferring control to newly
does not have an instruction cache, the flushi instruction updated instruction memory.
performs no operation. Operation: Flushes the processor pipeline of any
Operation: Flushes the instruction cache line associated prefetched instructions.
with address rA. Assembler Syntax: flushp
Assembler Syntax: flushi rA Example: flushp
Example: flushi r6 Exceptions: None
Exceptions: None Instruction Type: R
Instruction Type: R Instruction Fields: None
Instruction Fields: A = Register index of operand rA
initd (D1) initda (D2)
initialize data cache line initialize data cache address
Description: If the NiosII processor implements a direct Description: If the NiosII processor implements a direct

70
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

mapped data cache, initd clears the data cache line without mapped data cache, initda clears the data cache line without
checking for (or writing) a dirty data cache line that is checking for (or writing) a dirty data cache line that is
mapped to the specified address back to memory. Unlike mapped to the specified address back to memory. Unlike
initda, initd clears the cache line regardless of whether the initd, initda clears the cache line only when the addressed
addressed data is currently cached. This process comprises data is currently cached. This process comprises the
the following steps: following steps:
■ Compute the effective address specified by the sum of rA ■ Compute the effective address specified by the sum of rA
and the signed 16-bit immediate value. and the signed 16-bit immediate value.
■ Identify the data cache line associated with the computed ■ Identify the data cache line associated with the computed
effective address. Each data cache effective address effective address. Each data cache effective address
comprises a tag field and a line field. When identifying the comprises a tag field and a line field. When identifying the
line, initd ignores the tag field and only uses the line field line, initda uses both the tag field and the line field.
to select the data cache line to clear. ■ Compare the cache line tag with the effective address to
■ Skip comparing the cache line tag with the effective determine if the addressed data is currently cached. If the
address to determine if the addressed data is currently tag fields do not match, the effective address is not
cached. Because initd ignores the cache line tag, initd currently cached, so the instruction does nothing.
flushes the cache line regardless of whether the specified ■ Skip checking if the data cache line is dirty. Because
data location is currently cached. initd skips the dirty cache line check, data that has been
■ Skip checking if the data cache line is dirty. Because modified by the processor, but not yet written to memory is
initd skips the dirty cache line check, data that has been lost.
modified by the processor, but not yet written to memory is ■ Clear the valid bit for the line.
lost. If the NiosII processor core does not have a data cache, the
■ Clear the valid bit for the line. initda instruction performs no operation.
If the NiosII processor core does not have a data cache, the Usage: Use initda to skip writing dirty lines back to
initd instruction performs no operation. memory and to flush the cache line only if the addressed
Usage: Use initd after processor reset and before accessing memory location is currently in the cache.
data memory to initialize the processor’s data cache. Use Operation: Initializes the data cache line currently caching
initd with caution because it does not write back dirty data. address rA+ σ (IMM16)
Operation: Initializes the data cache line associated with Assembler Syntax: initda IMM16(rA)
address rA+ σ (IMM16). Example: initda -100(r6)
Assembler Syntax: initd IMM16(rA) Exceptions: Supervisor-only data address
Example: initd 0(r6) Fast TLB miss (data)
Exceptions: Supervisor-only instruction Double TLB miss (data)
Instruction Type: I MPU region violation (data)
Instruction Fields: A = Register index of operand rA Unimplemented instruction
IMM16 = 16-bit signed immediate value Instruction Type: I
Instruction Fields: A = Register index of operand rA
IMM16 = 16-bit signed immediate value
initi (D3) jmp (D4)
initialize instruction cache line computed jump
Description: Ignoring the tag, initi identifies the instruction Description: Transfers execution to the address contained
cache line associated with the byte address in ra, and initi in register rA.
invalidates that line. If the NiosII processor core does not Usage: It is illegal to jump to the address contained in
have an instruction cache, the initi instruction performs no register r31. To return from subroutines called by call or
operation. callr, use ret instead of jmp.
Usage: This instruction is used to initialize the processor’s Operation: PC ← rA
instruction cache. Immediately after processor reset, use Assembler Syntax: jmp rA
initi to invalidate each line of the instruction cache. Example: jmp r12
Operation: Initializes the instruction cache line associated Exceptions: Misaligned destination address
with address rA. Instruction Type: R
Assembler Syntax: initi rA Instruction Fields: A = Register index of operand rA
Example: initi r6
Exceptions: Supervisor-only instruction
Instruction Type: R
Instruction Fields: A = Register index of operand rA
jmpi (D5) ldb / ldbio (D6)
jump immediate load byte from memory or I/O peripheral

71
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

Description: Transfers execution to the instruction at Description: Computes the effective byte address specified
address (PC31…28 : IMM26 × 4). by the sum of rA and the instruction's signed 16-bit
Usage: jmpi is a low-overhead local jump. jmpi can immediate value. Loads register rB with the desired
transfer execution anywhere within the 256-MB range memory byte, sign extending the 8-bit value to 32 bits. In
determined by PC31..28. The NiosII GNU linker does not NiosII processor cores with a data cache, this instruction
automatically handle cases in which the address is out of may retrieve the desired data from the cache instead of
this range. from memory.
Operation: PC ← (PC31..28 : IMM26 × 4) Usage: Use the ldbio instruction for peripheral I/O. In
Assembler Syntax: jmpi label processors with a data cache, ldbio bypasses the cache and
Example: jmpi write_char is guaranteed to generate an Avalon-MM data transfer. In
Exceptions: None processors without a data cache, ldbio acts like ldb.
Instruction Type: J Operation: rB ← σ (Mem8[rA + σ (IMM16)])
Instruction Fields: Assembler Syntax: ldb rB, byte_offset(rA)
IMM26 = 26-bit unsigned immediate value ldbio rB, byte_offset(rA)
Example: ldb r6, 100(r5)
Exceptions: Supervisor-only data address
Misaligned data address
TLB permission violation (read)
Fast TLB miss (data)
Double TLB miss (data)
MPU region violation (data)
Instruction Type: I
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
ldbu / ldbuio (D7) ldh / ldhio (D8)
load unsigned byte from memory or I/O peripheral load halfword from memory or I/O peripheral
Description: Computes the effective byte address specified Description: Computes the effective byte address specified
by the sum of rA and the instruction's signed 16-bit by the sum of rA and the instruction's signed 16-bit
immediate value. Loads register rB with the desired immediate value. Loads register rB with the memory
memory byte, zero extending the 8-bit value to 32 bits. halfword located at the effective byte address, sign
Usage: In processors with a data cache, this instruction extending the 16-bit value to 32 bits. The effective byte
may retrieve the desired data from the cache instead of address must be halfword aligned. If the byte address is not
from memory. Use the ldbuio instruction for peripheral I/O. a multiple of 2, the operation is undefined.
In processors with a data cache, ldbuio bypasses the cache Usage: In processors with a data cache, this instruction
and is guaranteed to generate an Avalon-MM data transfer. may retrieve the desired data from the cache instead of
In processors without a data cache, ldbuio acts like ldbu. from memory. Use the ldhio instruction for peripheral I/O.
Operation: rB ← 0x000000 : Mem8[rA + σ (IMM16)] In processors with a data cache, ldhio bypasses the cache
Assembler Syntax: ldbu rB, byte_offset(rA) and is guaranteed to generate an Avalon-MM data transfer.
ldbuio rB, byte_offset(rA) In processors without a data cache, ldhio acts like ldh.
Example: ldbu r6, 100(r5) Operation: rB ← σ (Mem16[rA + σ (IMM16)])
Exceptions: Supervisor-only data address Assembler Syntax: ldh rB, byte_offset(rA)
Misaligned data address ldhio rB, byte_offset(rA)
TLB permission violation (read) Example: ldh r6, 100(r5)
Fast TLB miss (data) Exceptions: Supervisor-only data address
Double TLB miss (data) Misaligned data address
MPU region violation (data) TLB permission violation (read)
Instruction Type: I Fast TLB miss (data)
Instruction Fields: A = Register index of operand rA Double TLB miss (data)
B = Register index of operand rB MPU region violation (data)
IMM16 = 16-bit signed immediate value Instruction Type: I
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
ldhu / ldhuio (D9) ldw / ldwio (D10)
load unsigned halfword from memory or I/O peripheral load 32 bit word from memory or I/O peripheral
Description: Computes the effective byte address specified Description: Computes the effective byte address specified

72
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

by the sum of rA and the instruction's signed 16-bit by the sum of rA and the instruction's signed 16-bit
immediate value. Loads register rB with the memory immediate value. Loads register rB with the memory word
halfword located at the effective byte address, zero located at the effective byte address. The effective byte
extending the 16-bit value to 32 bits. The effective byte address must be word aligned. If the byte address is not a
address must be halfword aligned. If the byte address is not multiple of 4, the operation is undefined.
a multiple of 2, the operation is undefined. Usage: In processors with a data cache, this instruction
Usage: In processors with a data cache, this instruction may retrieve the desired data from the cache instead of
may retrieve the desired data from the cache instead of from memory. Use the ldwio instruction for peripheral I/O.
from memory. Use the ldhuio instruction for peripheral I/O. In processors with a data cache, ldwio bypasses the cache
In processors with a data cache, ldhuio bypasses the cache and memory. Use the ldwio instruction for peripheral I/O.
and is guaranteed to generate an Avalon-MM data transfer. In processors with a data cache, ldwio bypasses the cache
In processors without a data cache, ldhuio acts like ldhu. and is guaranteed to generate an Avalon-MM data transfer.
Operation: rB ←0x0000 : Mem16[rA + σ (IMM16)] In processors without a data cache, ldwio acts like ldw.
Assembler Syntax: ldhurB, byte_offset(rA) Operation: rB ← Mem32[rA + σ (IMM14)]
ldhuio rB, byte_offset(rA) Assembler Syntax: ldw rB, byte_offset(rA)
Example: ldhu r6, 100(r5) ldwio rB, byte_offset(rA)
Exceptions: Supervisor-only data address Example: ldw r6, 100(r5)
Exceptions: Supervisor-only data address Exceptions: Supervisor-only data address
Misaligned data address Misaligned data address
TLB permission violation (read) TLB permission violation (read)
Fast TLB miss (data) Fast TLB miss (data)
Double TLB miss (data) Double TLB miss (data)
MPU region violation (data) MPU region violation (data)
Instruction Type: I Instruction Type: I
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
IMM16 = 16-bit signed immediate value IMM16 = 16-bit signed immediate value
mov (D11) movhi (D12)
move register to register move immediate into high halfword
Description: Moves the contents of rA to rC. Description: Writes the immediate value IMMED into the
Operation: rC ← rA high halfword of rB, and clears the lower halfword of rB to
Assembler Syntax: mov rC,rA 0x0000.
Example: mov r6,r7 Usage: The maximum allowed value of IMMED is 65535.
Pseudo-instruction: mov is implemented as add rC, rA, r0. The minimum allowed value is 0. To load a 32-bit constant
movi (D13) into a register, first load the upper 16 bits using a movhi
move signed immediate into word pseudo-instruction. The%hi() macro can be used to extract
Description: Sign-extends the immediate value IMMED to the upper 16 bits of a constant or a label. Then, load the
32 bits and writes it to rB. lower 16 bits with an ori instruction. The%lo() macro can
Usage: The maximum allowed value of IMMED is 32767. be used to extract the lower 16 bits of a constant or label as
The minimum allowed value is –32768. To load a 32-bit shown below.
constant into a register, refer to the movhi instruction. movhi rB, %hi (value)
Operation: rB ← σ (IMMED) ori rB, rB, %lo (value)
Assembler Syntax: movi rB,IMMED An alternative method to load a 32-bit constant into a
Example: movi r6,-30 register uses the%hiadj() macro and the addi instruction as
Pseudo-instruction: movi is implemented as shown below.
addi rB, r0, IMMED. movhi rB, %hiadj (value)
addi rB, rB, %lo (value)
Operation: rB ← (IMMED : 0x0000)
Assembler Syntax: movhi rB,IMMED
Example: movhi r6,0x8000
Pseudo-instruction: movhi is implemented as
orhi rB, r0, IMMED.
movia (D14) mul (D16)
move immediate address into word multiply
Description: Writes the address of label to rB. Description: Multiplies rA times rB and stores the 32 low-
Operation: rB ←label order bits of the product to rC. The result is the same
Assembler Syntax: movia rB,label whether the operands are treated as signed or unsigned

73
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

Example: movia r6, function_address integers.


Pseudo-instruction: movia is implemented as: NiosII processors that do not implement the mul instruction
orhi rB, r0, %hiadj(label) cause an unimplemented instruction exception.
addi rB, rB, %lo(label) Usage: Carry Detection (unsigned operands) and
movui (D15) Overflow Detection (signed operands).
move unsigned immediate into word Operation: rC ← (rA × rB) 31..0
Description: Zero-extends the immediate value IMMED to Assembler Syntax: mul rC, rA, rB
32 bits and writes it to rB. Example: mul r6, r7, r8
Usage: The maximum allowed value of IMMED is 65535. Exceptions: Unimplemented instruction
The minimum allowed value is 0. To load a 32-bit constant Instruction Type: R
into a register, refer to the movhi instruction. Instruction Fields: A = Register index of operand rA
Operation: rB ←(0x0000 : IMMED) B = Register index of operand rB
Assembler Syntax: movui rB, IMMED C = Register index of operand rC
Example: movui r6, 100
Pseudo-instruction: movui is implemented as
ori rB, r0, IMMED
muli (E1) mulxss (E2)
multiply immediate multiply extended signed/signed
Description: Sign-extends the 16-bit immediate value Description: Treating rA and rB as signed integers, mulxss
IMM16 to 32 bits and multiplies it by the value of rA. multiplies rA times rB, and stores the 32 high-order bits of
Stores the 32 low-order bits of the product to rB. The result the product to rC.
is independent of whether rA is treated as a signed or NiosII processors that do not implement the mulxss
unsigned number. instruction cause an unimplemented instruction exception.
NiosII processors that do not implement the muli Usage: Use mulxss and mul to compute the full 64-bit
instruction cause an unimplemented instruction exception. product of two 32-bit signed integers. Furthermore, mulxss
Operation: rB ← (rA × σ (IMM16)) 31..0 can be used as part of the calculation of a 128-bit product
Assembler Syntax: muli rB, rA, IMM16 of two 64-bit signed integers.
Example: muli r6, r7, -100 Operation: rC ← ((signed) rA) × ((signed) rB)) 63..32
Exceptions: Unimplemented instruction Assembler Syntax: mulxss rC, rA, rB
Instruction Type: I Example: mulxss r6, r7, r8
Instruction Fields: A = Register index of operand rA Exceptions: Unimplemented instruction
B = Register index of operand rB Instruction Type: R
IMM16 = 16-bit signed immediate value Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
C = Register index of operand rC
mulxsu (E3) mulxuu (E4)
multiply extended signed/unsigned multiply extended unsigned/unsigned
Description: Treating rA as a signed integer and rB as an Description: Treating rA and rB as unsigned integers,
unsigned integer, mulxsu multiplies rA times rB, and stores mulxuu multiplies rA times rB and stores the 32 high-order
the 32 high-order bits of the product to rC. bits of the product to rC.
NiosII processors that do not implement the mulxsu NiosII processors that do not implement the mulxuu
instruction cause an unimplemented instruction exception. instruction cause an unimplemented instruction exception.
Usage: mulxsu can be used as part of the calculation of a Usage: Use mulxuu and mul to compute the 64-bit product
128-bit product of two 64-bit signed integers. of two 32-bit unsigned integers. Furthermore, mulxuu can
Operation: rC ← ((signed) rA) × ((unsigned) rB)) 63..32 be used as part of the calculation of a 128-bit product of
Assembler Syntax: mulxsu rC, rA, rB two 64-bit signed integers.
Example: mulxsu r6, r7, r8 Operation:
Exceptions: Unimplemented instruction rC ← ((unsigned) rA) × ((unsigned) rB)) 63..32
Instruction Type: R Assembler Syntax: mulxuu rC, rA, rB
Instruction Fields: A = Register index of operand rA Example: mulxuu r6, r7, r8
B = Register index of operand rB Exceptions: Unimplemented instruction
C = Register index of operand rC Instruction Type: R
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
C = Register index of operand rC
nextpc (E5) nop (E6)

74
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

get address of following instruction no operation


Description: Stores the address of the next instruction to Description: nop does nothing.
register rC. Operation: None
Usage: A relocatable code fragment can use nextpc to Assembler Syntax: nop
calculate the address of its data segment. nextpc is the only Example: nop
way to access the PC directly. Pseudo-instruction: nop is implemented as
Operation: rC ← PC + 4 add r0, r0, r0.
Assembler Syntax: nextpc rC
Example: nextpc r6
Exceptions: None
Instruction Type: R
Instruction Fields: C = Register index of operand rC
nor (E7) or (E8)
bitwise logical nor bitwise logical or
Description: Calculates the bitwise logical NOR of rA and Description: Calculates the bitwise logical OR of rA and
rB and stores the result in rC. rB and stores the result in rC.
Operation: rC ← ~(rA | rB) Operation: rC ← rA | rB
Assembler Syntax: nor rC, rA, rB Assembler Syntax: or rC, rA, rB
Example: nor r6, r7, r8 Example: or r6, r7, r8
Exceptions: None Exceptions: None
Instruction Type: R Instruction Type: R
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
C = Register index of operand rC C = Register index of operand rC
orhi (E9) ori (E10)
bitwise logical or immediate into high halfword bitwise logical or immediate
Description: Calculates the bitwise logical OR of rA and Description: Calculates the bitwise logical OR of rA and
(IMM16 : 0x0000) and stores the result in rB. (0x0000 : IMM16) and stores the result in rB.
Operation: rB ← rA | (IMM16 : 0x0000) Operation: rB ← rA | (0x0000 : IMM16)
Assembler Syntax: orhi rB, rA, IMM16 Assembler Syntax: ori rB, rA, IMM16
Example: orhi r6, r7, 100 Example: ori r6, r7, 100
Exceptions: None Exceptions: None
Instruction Type: I Instruction Type: I
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
IMM16 = 16-bit signed immediate value IMM16 = 16-bit unsigned immediate value
rdctl (E11) rdprs (E12)
read from control register read from previous register set
Description: Reads the value contained in control register Description: Sign-extends the 16-bit immediate value
ctlN and writes it to register rC. IMM16 to 32 bits, and adds it to the value of rA from the
Operation: rC ← ctlN previous register set. Places the result in rB in the current
Assembler Syntax: rdctl rC, ctlN register set.
Example: rdctl r3, ctl31 Usage: The previous register set is specified by status.PRS.
Exceptions: Supervisor-only instruction By default, status.PRS indicates the register set in use
Instruction Type: R before an exception, such as an external interrupt, caused a
Instruction Fields: register set change.
C = Register index of operand rC To read from an arbitrary register set, software can insert
N = Control register index of operand ctlN the desired register set number in status.PRS prior to
ret (E13) executing rdprs.
return from subroutine If shadow register sets are not implemented on the NiosII
Description: Transfers execution to the address in ra. core, rdprs is an illegal instruction.
Usage: Any subroutine called by call or callr must use ret Operation: rB ← prs.rA + σ (IMM16)
to return. Assembler Syntax: rdprs rB, rA, IMM16
Operation: PC ← ra Example: rdprs r6, r7, 0
Assembler Syntax: ret Exceptions: Supervisor-only instruction
Example: ret Illegal instruction

75
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

Exceptions: Misaligned destination address Instruction Type: I


Instruction Type: R Instruction Fields: A = Register index of operand rA
Instruction Fields: None B = Register index of operand rB
IMM16 = 16-bit signed immediate value
rol (E14) roli (E15)
rotate left rotate left immediate
Description: Rotates rA left by the number of bits Description: Rotates rA left by the number of bits
specified in rB4..0 and stores the result in rC. The bits that specified in IMM5 and stores the result in rC. The bits that
shift out of the register rotate into the least-significant bit shift out of the register rotate into the least-significant bit
positions. Bits 31–5 of rB are ignored. positions.
Operation: rC ← rA rotated left rB4..0 bit positions Usage: In addition to the rotate-left operation, roli can be
Assembler Syntax: rol rC, rA, rB used to implement a rotate-right operation.
Example: rol r6, r7, r8 Rotating left by (32 – IMM5) bits is the equivalent of
Exceptions: None rotating right by IMM5 bits.
Instruction Type: R Operation: rC ← rA rotated left IMM5 bit positions
Instruction Fields: A = Register index of operand rA Assembler Syntax: roli rC, rA, IMM5
B = Register index of operand rB Example: roli r6, r7, 3
C = Register index of operand rC Exceptions: None
Instruction Type: R
Instruction Fields: A = Register index of operand rA
C = Register index of operand rC
IMM5 = 5-bit unsigned immediate value
ror (E16) sll (F1)
rotate right shift left logical
Description: Rotates rA right by the number of bits Description: Shifts rA left by the number of bits specified
specified in rB4..0 and stores the result in rC. The bits that in rB4..0 (inserting zeroes), and then stores the result in rC.
shift out of the register rotate into the most-significant bit sll performs the << operation of the C programming
positions. Bits 31– 5 of rB are ignored. language.
Operation: rC ← rA rotated right rB4..0 bit positions Operation: rC ←rA << (rB4..0)
Assembler Syntax: ror rC, rA, rB Assembler Syntax: sll rC, rA, rB
Example: ror r6, r7, r8 Example: sll r6, r7, r8
Exceptions: None Exceptions: None
Instruction Type: R Instruction Type: R
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
C = Register index of operand rC C = Register index of operand rC
slli (F2) sra (F3)
shift left logical immediate shift right arithmetic
Description: Shifts rA left by the number of bits specified Description: Shifts rA right by the number of bits specified
in IMM5 (inserting zeroes), and then stores the result in rC. in rB4..0 (duplicating the sign bit), and then stores the
Usage: slli performs the << operation of the C result in rC. Bits 31–5 are ignored.
programming language. Usage: sra performs the signed >> operation of the C
Operation: rC ← rA << IMM5 programming language.
Assembler Syntax: slli rC, rA, IMM5 Operation:
Example: slli r6, r7, 3 rC ← (signed) rA >> ((unsigned) rB4..0)
Exceptions: None Assembler Syntax: sra rC, rA, rB
Instruction Type: R Example: sra r6, r7, r8
Instruction Fields: A = Register index of operand rA Exceptions: None
C = Register index of operand rC Instruction Type: R
IMM5 = 5-bit unsigned immediate value Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
C = Register index of operand rC
srai (F4) srl (F5)
shift right arithmetic immediate shift right logical
Description: Shifts rA right by the number of bits specified Description: Shifts rA right by the number of bits specified
in IMM5 (duplicating the sign bit), and then stores the in rB4..0 (inserting zeroes), and then stores the result in rC.

76
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

result in rC. Bits 31–5 are ignored.


Usage: srai performs the signed >> operation of the C Usage: srl performs the unsigned >> operation of the C
programming language. programming language.
Operation: Operation:
rC ←(signed) rA >> ((unsigned) IMM5) rC ←(unsigned) rA >> ((unsigned) rB4..0)
Assembler Syntax: srai rC, rA, IMM5 Assembler Syntax: srl rC, rA, rB
Example: srai r6, r7, 3 Example: srl r6, r7, r8
Exceptions: None Exceptions: None
Instruction Type: R Instruction Type: R
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
C = Register index of operand rC B = Register index of operand rB
IMM5 = 5-bit unsigned immediate value C = Register index of operand rC
srli (F6) stb / stbio (F7)
shift right logical immediate store byte to memory or I/O peripheral
Description: Shifts rA right by the number of bits specified Description: Computes the effective byte address specified
in IMM5 (inserting zeroes), and then stores the result in rC. by the sum of rA and the instruction's signed 16-bit
Usage: srli performs the unsigned >> operation of the C immediate value. Stores the low byte of rB to the memory
programming language. byte specified by the effective address.
Operation: Usage: In processors with a data cache, this instruction
rC ←(unsigned) rA >> ((unsigned) IMM5) may not generate an Avalon-MM bus cycle to noncache
Assembler Syntax: srli rC, rA, IMM5 data memory immediately. Use the stbio instruction for
Example: srli r6, r7, 3 peripheral I/O. In processors with a data cache, stbio
Exceptions: None bypasses the cache and is guaranteed to generate an
Instruction Type: R Avalon-MM data transfer. In processors without a data
Instruction Fields: A = Register index of operand rA cache, stbio acts like stb.
C = Register index of operand rC Operation:
IMM5 = 5-bit unsigned immediate value Mem8[rA + σ (IMM16)] ← rB7..0
Assembler Syntax: stb rB, byte_offset(rA)
stbio rB, byte_offset(rA)
Example: stb r6, 100(r5)
Exceptions: Supervisor-only data address
Misaligned data address
TLB permission violation (write)
Fast TLB miss (data)
Double TLB miss (data)
MPU region violation (data)
Instruction Type: I
Instruction Fields: A = Register index of operand rA
B = Register index of operand rB
IMM16 = 16-bit signed immediate value
sth / sthio (F8) stw / stwio (F9)
store halfword to memory or I/O peripheral store word to memory or I/O peripheral
Description: Computes the effective byte address specified Description: Computes the effective byte address specified
by the sum of rA and the instruction's signed 16-bit by the sum of rA and the instruction's signed 16-bit
immediate value. Stores the low halfword of rB to the immediate value. Stores rB to the memory location
memory location specified by the effective byte address. specified by the effective byte address. The effective byte
The effective byte address must be halfword aligned. If the address must be word aligned. If the byte address is not a
byte address is not a multiple of 2, the operation is multiple of 4, the operation is undefined.
undefined. Usage: In processors with a data cache, this instruction
Usage: In processors with a data cache, this instruction may not generate an Avalon-MM data transfer
may not generate an Avalon-MM data transfer immediately. Use the stwio instruction for peripheral I/O.
immediately. Use the sthio instruction for peripheral I/O. In In processors with a data cache, stwio bypasses the cache
processors with a data cache, sthio bypasses the cache and and is guaranteed to generate an Avalon-MM bus cycle. In
is guaranteed to generate an Avalon-MM data transfer. In processors without a data cache, stwio acts like stw.
processors without a data cache, sthio acts like sth. Operation:
Operation: Mem32[rA + σ (IMM16)] ←rB
Mem16[rA + σ (IMM16)] ←rB15..0 Assembler Syntax: stw rB, byte_offset(rA)

77
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

Assembler Syntax: sth rB, byte_offset(rA) stwio rB, byte_offset(rA)


sthio rB, byte_offset(rA) Example: stw r6, 100(r5)
Example: sth r6, 100(r5) Exceptions: Supervisor-only data address
Exceptions: Supervisor-only data address Misaligned data address
Misaligned data address TLB permission violation (write)
TLB permission violation (write) Fast TLB miss (data)
Fast TLB miss (data) Double TLB miss (data)
Double TLB miss (data) MPU region violation (data)
MPU region violation (data) Instruction Type: I
Instruction Type: I Instruction Fields: A = Register index of operand rA
Instruction Fields: A = Register index of operand rA B = Register index of operand rB
B = Register index of operand rB IMM16 = 16-bit signed immediate value
IMM16 = 16-bit signed immediate value
sub (F10) subi (F11)
subtract subtract immediate
Description: Subtract rB from rA and store the result in rC. Description: Sign-extends the immediate value IMMED to
Usage: Carry Detection (unsigned operands) and Overflow 32 bits, subtracts it from the value of rA and then stores the
Detection (signed operands) result in rB.
Operation: rC ← rA–rB Usage: The maximum allowed value of IMMED is 32768.
Assembler Syntax: sub rC, rA, rB The minimum allowed value is –32767.
Example: sub r6, r7, r8 Operation: rB ← rA– σ (IMMED)
Exceptions: None Assembler Syntax: subi rB, rA, IMMED
Instruction Type: R Example: subi r8, r8, 4
Instruction Fields: A = Register index of operand rA Pseudo-instruction: subi is implemented as addi rB, rA, -
B = Register index of operand rB IMMED
C = Register index of operand rC
sync (F12) trap (F13)
memory synchronization trap
Description: Forces all pending memory accesses to Operation:
complete before allowing execution of subsequent estatus ←status
instructions. In processor cores that support in-order PIE ←0
memory accesses only, this instruction performs no U ←0
operation. ea ←PC+4
Operation: None PC ←exception handler address
Assembler Syntax: sync Description: Saves the address of the next instruction in
Example: sync register ea, saves the contents of the status register in
Exceptions: None estatus, disables interrupts, and transfers execution to the
Instruction Type: R exception handler. The address of the exception handler is
Instruction Fields: None specified at system generation time.The 5-bit immediate
wrctl (F14) field imm5 is ignored by the processor, but it can be used
write to control register by the debugger.
Description: Writes the value contained in register rA to trap with no argument is the same as trap 0.
the control register ctlN. Usage: To return from the exception handler, execute an
Operation: ctlN ←rA eret instruction.
Assembler Syntax: wrctl ctlN, rA Assembler Syntax: trap
Example: wrctl ctl6, r3 trap imm5
Exceptions: Supervisor-only instruction Example: trap
Instruction Type: R Exceptions: Trap
Instruction Fields: Instruction Type: R
A = Register index of operand rA Instruction Fields: IMM5 = Type of breakpoint
N = Control register index of operand ctlN
wrprs (F15) xor (F16)
write to previous register set bitwise logical exclusive or

78
Khoa Điện tử – Viễn thông THỰC HÀNH CẤU TRÚC MÁY TÍNH
Bộ môn Máy tính – Hệ thống nhúng

Description: Copies the value of rA in the current register Description: Calculates the bitwise logical exclusive-or of
set to rC in the previous register set. This instruction can rA and rB and stores the result in rC.
set r0 to 0 in a shadow register set. Operation: rC ← rA ^ rB
Usage: The previous register set is specified by status.PRS. Assembler Syntax: xor rC, rA, rB
By default, status.PRS indicates the register set in use Example: xor r6, r7, r8
before an exception, such as an external interrupt, caused a Exceptions: None
register set change. Instruction Type: R
To write to an arbitrary register set, software can insert the Instruction Fields: A = Register index of operand rA
desired register set number in status.PRS prior to executing B = Register index of operand rB
wrprs. C = Register index of operand rC
System software must use wrprs to initialize r0 to 0 in each
shadow register set before using that register set. If shadow
register sets are not implemented on the NiosII core, wrprs
is an illegal instruction.
Operation: prs.rC ← rA
Assembler Syntax: wrprs rC, rA
Example: wrprs r6, r7
Exceptions: Supervisor-only instruction
Illegal instruction
Instruction Type: R
Instruction Fields: A = Register index of operand rA
C = Register index of operand rC
xorhi (G1)
xori (G2)
bitwise logical exclusive or immediate into high
bitwise logical exclusive or immediate
halfword
Description: Calculates the bitwise logical exclusive XOR Description: Calculates the bitwise logical exclusive OR of
of rA and (IMM16 : 0x0000) and stores the result in rB. rA and (0x0000 : IMM16) and stores the result in rB.
Operation: rB ← rA ^ (IMM16 : 0x0000) Operation: rB ← rA ^ (0x0000 : IMM16)
Assembler Syntax: xorhi rB, rA, IMM16 Assembler Syntax: xori rB, rA, IMM16
Example: xorhi r6, r7, 100 Example: xori r6, r7, 100
Exceptions: None Exceptions: None
Instruction Type: I Instruction Type: I
Instruction Fields: A = Register index of operand rA Instruction Fields: A = Register index of operand rA
B = Register index of operand rB B = Register index of operand rB
IMM16 = 16-bit unsigned immediate value IMM16 = 16-bit unsigned immediate value

79