AL/AHレジスタを使ってマスク処理をアセンブラで書いてみます。
まずは、代入するだけの処理。
変数aに0x12345678を代入します。
takk@deb9:~/tmp$ cat -n t.c
1 #include <stdio.h>
2 int main()
3 {
4 unsigned long a;
5 asm("movq $0x12345678,-0x8(%rbp)");
6 printf("%08x\n",a);
7 }
takk@deb9:~/tmp$
実行。
takk@deb9:~/tmp$ gcc t.c takk@deb9:~/tmp$ ./a.out 12345678 takk@deb9:~/tmp$
さきほどのプログラムをベースに、ALレジスタを用いて下位8bitを0x00にする処理を、作ってみます。
ローカル変数をEAXにコピーして、ALを0にした後、EAXをローカル変数に戻すだけです。
takk@deb9:~/tmp$ cat -n t.c
1 #include <stdio.h>
2 int main()
3 {
4 unsigned long a;
5 asm("movq $0x12345678,-0x8(%rbp)");
6 asm("mov -0x8(%rbp), %eax");
7 asm("mov $0,%al");
8 asm("mov %eax,-0x8(%rbp)");
9 printf("%08x\n",a);
10 }
takk@deb9:~/tmp$ gcc t.c
takk@deb9:~/tmp$ ./a.out
12345600
takk@deb9:~/tmp$
次は、bit8~15をマスク。AHレジスタに0xFFを入れます。
takk@deb9:~/tmp$ cat -n t.c
1 #include <stdio.h>
2 int main()
3 {
4 unsigned long a;
5 asm("movq $0x12345678,-0x8(%rbp)");
6 asm("mov -0x8(%rbp), %eax");
7 asm("mov $0xFF,%ah");
8 asm("mov %eax,-0x8(%rbp)");
9 printf("%08x\n",a);
10 }
takk@deb9:~/tmp$ gcc t.c
takk@deb9:~/tmp$ ./a.out
1234ff78
takk@deb9:~/tmp$
コツを掴んできました。
次は上位16bitをオール0にします。
takk@deb9:~/tmp$ cat -n t.c
1 #include <stdio.h>
2 int main()
3 {
4 unsigned long a;
5 asm("movq $0x12345678,-0x8(%rbp)");
6 asm("mov -0x8(%rbp), %eax");
7 asm("mov $0,%ebx");
8 asm("mov %ax,%bx");
9 asm("mov %ebx,-0x8(%rbp)");
10 printf("%08x\n",a);
11 }
takk@deb9:~/tmp$ gcc t.c
takk@deb9:~/tmp$ ./a.out
00005678
takk@deb9:~/tmp$


コメント