アセンブリのお勉強
検索と置換!
転送命令、分岐命令、比較命令があれば、どんな演算でも新たに作ることができます!
以下では、Intel syntax に従います。また、CPU は 16bit とします。
0.Instruction
Instruction = Opcode (Operation Code) + Operands
Operand:immediate value, register, memory, I/O port
1.Move Instruction (Transfer Instruction)
1-1.Unconditional Move Instruction
mov destination, source ;transfer source to destination
1-2.Conditional Move Instruction
cmoveとcmovz、cmovneとcmovnzのマシーンコードは同じです。
cmove destination, source ;zf == 1 → transfer source to destination
cmovne destination, source ;zf == 0 → transfer source to destination
1-3.PUSH Instruction and POP Instruction
PUSH命令及びPOP命令は、転送命令の一つです。
〈PUSH命令の流れ〉
0)PUSH命令の実行前にインストラクションポインタが命令長分だけインクリメントする。(PUSH命令の次の命令のアドレスがインストラクションポインタにセットされる)
1)PUSH命令が実行されると、スタックポインタがスタックに積み込むデータのサイズ(operand-size attribute)だけディクリメントする。
2)スタックポインタが指す位置にオペランド(source)のサイズだけデータが積み込まれる。
〈POP命令の流れ〉
0)POP命令の実行前にインストラクションポインタが命令長分だけインクリメントする。(POP命令の次の命令のアドレスがインストラクションポインタにセットされる)
1)POP命令が実行されると、スタックポインタが指す位置からオペランド(destination)のサイズだけデータが取り出される。
2)スタックポインタがスタックから取り出したデータのサイズ(operand-size attribute)だけインクリメントする。
push source ;transfer source to [sp]
pop destination ;transfer [sp] to destination
1-4.I/O Instruction
I/O命令は、CPUのレジスタとI/Oポート間の転送命令です。destination 及び source にはレジスタを指定し、port-address は、即値またはレジスタで指定します。 尚、[port-address] は、port-address で指定されたI/Oポートを表します。
in destination, port-address ;transfer [port-address] to destination
out port-address, source ;transfer source to [port-address]
2.Jump Instruction (Branch Instruction)
ジャンプ命令は、インストラクションポインタ(ip)への転送命令です。
2-1.Unconditional Jump Instruction
jmp source ;set ip to source
2-2.Conditional Jump Instruction
jeとjz、jneとjnzのマシーンコードは同じです。
je source ;zf == 1 → set ip to source
jne source ;zf == 0 → set ip to source
jcxz は、cx が 0 でなければ何もせず、cx が 0 になればオペランドにジャンプします。
start:
mov cx 10
loop:
・・・・・・・
dec cx
jcxz break ;cx == 0 → jump to break
jmp loop:
break:
・・・・・・・
2-3.Unconditional Loop Instruction with Count Register
loop は、cx をディクリメントした後、cx が 0 でなければオペランドにジャンプし、cx が 0 になればループを抜けます。
loop source ;cx--, cx != 0 → set ip to source
start:
mov cx 10
label:
・・・・・・・
loop label
・・・・・・・
2-4.Conditional Loop Instruction with Count Register
loopeとloopz、loopneとloopnzのマシーンコードは同じです。
loope source ;cx--, cx != 0 and zf == 1 → set ip to source
loopne source ;cx--, cx != 0 and zf == 0 → set ip to source
2-5.CALL Instruction and RET Instruction
CALL命令及びRET命令は、ジャンプ命令の一つです。
〈CALL命令の流れ〉
0)CALL命令の実行前にインストラクションポインタが命令長分だけインクリメントする。(CALL命令の次の命令のアドレスがインストラクションポインタにセットされる)
1)CALL命令が実行されると、インストラクションポインタの値がスタックに転送される。(インストラクションポインタがプッシュされる)
2)呼び出し先のアドレス(CALL命令のオペランド)がインストラクションポインタにセットされる。
〈RET命令の流れ〉
0)RET命令の実行前にインストラクションポインタが命令長分だけインクリメントする。(RET命令の次の命令のアドレスがインストラクションポインタにセットされる)
1)RET命令が実行されると、スタックの値がインストラクションポインタに転送される。(インストラクションポインタにポップされる)
サブルーチンでレジスタを使用するときは、次の例ようにPUSH命令で使用するレジスタの値を一旦スタックに退避(save)させておき、レジスタを使い終わったらPOP命令で元の値に復元(restore)します。 また、PUSHA命令、POPA命令は、それぞれ全てのレジスタをPUSH、POSする命令です。
start:
call sub1
call sub2
sub1:
push ax
push bx
push cx
mov ax, 0
mov bx, 0
mov cx, 0
pop cx
pop bx
pop ax
ret
sub2:
pusha
・・・・・・・
popa
ret
次の例では、スタックを使ってサブルーチンに値を渡しています。(レジスタやメモリを使ってサブルーチンに値を渡すこともできます)
main:
push 3 ;sp-=2
push 2 ;sp-=2
push 1 ;sp-=2
call addxyz ;sp-=2
addxyz:
push bp ;sp-=2
mov bp, sp
mov ax, [bp+4]
add ax, [bp+6]
add ax, [bp+8]
mov sp, bp
pop bp
ret
short addxyz(short x, short y, short z) {
return x + y + z;
}
int main(void) {
addxyz(1, 2, 3);
}
2-6.Software Interrupt Instruction
ソフトウェア割り込み命令は、ジャンプ命令の一つです。INT命令及びIRET命令は、それぞれCALL命令及びRET命令に対応しています。
int (interrupt number)
int 0x21 ;DOS System Call
int 0x80 ;Linux System Call
iret
3.Compare Instruction
cmp ax, 0
cmp ax, bx
cmp ax, [0x0000]
cmp source1, source2
ZF | CF | |
---|---|---|
source1 < source2 | 0 | 1 |
source1 == source2 | 1 | 0 |
source1 > source2 | 0 | 0 |
X XNOR Y ≡ (X∧Y)∨(¬X∧¬Y)
4.Other Instructions
4-1.Increment Instruction and Decrement Instruction
inc state ;state + 1 -> state
dec state ;state - 1 -> state
4-2.Arithmetic Instruction
add state, source ;state + source -> state
sub state, source ;state - source -> state
mul state, source ;state * source -> state
div state, source ;state / source -> state
4-3.Logic Instruction
not state ;¬ state -> state
and state, source ;state ∧ source -> state
or state, source ;state ∨ source -> state
4-4.Bit Manipulation Instruction (Bit Shift Instruction and Bit Rotate Instruction)
shl state, count
shr state, count
rol state, count
ror state, count
4-5.No Operation Instruction and Halt Instruction
nop
hlt
Appendix
A-1.Addressing Mode
Address Space:memory address space, I/O address space
Addressing Mode:immediate addressing, absolute addressing, ralative addressing, direct addressing, indirect addressing
A-2.I/O Controller (Device Controller)
Processor ⇔ I/O Controller (Device Controller) ⇔ I/O Device (Peripheral Device)
I/O Controller:status register、command register (control register)、input register、output register
参考サイト
- x86 Assembly
- Assembly Programming
- x86 Instruction Set Reference
- NASM (Netwide Assembler)
- Central Processing Unit instructions set
- アセンブリ言語の基礎知識
- アセンブリ言語の基礎知識 Part5
- x86アセンブリ言語での関数コール
- ARM Instruction Sets
- Conditional Move Instructions
- CMP命令とフラグの変化
- 比較命令とジャンプ命令
- Jump and Branch Instructions
- LOOP/LOOPcc
- Bit Shift and Rotate Instructions
- 新・コンピュータ解体新書
- 独習アセンブラ
- Processor-I/O Controller Interactions