除法和32位差不多,毕竟背后的数学公式是一样的。区别只是32位的乘法需要两个寄存器来存放大数相乘的结果,而64位的不需要,一个寄存器就能存下。所以在64位的环境下,多了右移32位这条指令,其他指令一样。
//code
#include <stdio.h>
void i_c_c()
{
int num = 1234;
int bit = 0;
bit = num / 1000;
printf("bit = %d %d\n", bit, 1234 % 1000); // 1 234
}
int main()
{
i_c_c();
return 0;
}
// asm
#include <stdio.h>
void i_c_c()
{
/*1149: f3 0f 1e fa endbr64
114d: 55 push %rbp
114e: 48 89 e5 mov %rsp,%rbp
1151: 48 83 ec 10 sub $0x10,%rsp
1155: c7 45 f8 d2 04 00 00 movl $0x4d2,-0x8(%rbp)*/
int a = 1234;
/*115c: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)*/
int b = 0;
/*1163: 8b 45 f8 mov -0x8(%rbp),%eax // eax = 1234
1166: 48 63 d0 movslq %eax,%rdx // rdx = 1234
1169: 48 69 d2 d3 4d 62 10 imul $0x10624dd3,%rdx,%rdx // rdx = 1234*274877907 =339199337238 = 0x4E F9DB 2316
1170: 48 c1 ea 20 shr $0x20,%rdx // rdx = 0x4E
1174: c1 fa 06 sar $0x6,%edx // edx = 1 (2^38)/274877907 = 999.9999 = 1000
1177: c1 f8 1f sar $0x1f,%eax // eax = 0
117a: 29 c2 sub %eax,%edx // edx = 1
117c: 89 d0 mov %edx,%eax // edx = 1
117e: 89 45 fc mov %eax,-0x4(%rbp)*/
b = a / 1000;
/*1181: 8b 45 fc mov -0x4(%rbp),%eax // eax = 1
1184: ba ea 00 00 00 mov $0xea,%edx // edx = 0xea
1189: 89 c6 mov %eax,%esi
118b: 48 8d 3d 72 0e 00 00 lea 0xe72(%rip),%rdi # 2004 <_IO_stdin_used+0x4>
1192: b8 00 00 00 00 mov $0x0,%eax
1197: e8 b4 fe ff ff callq 1050 <printf@plt>*/
printf("%d %d\n", b, 0xea);
/*119c: 90 nop
119d: c9 leaveq
119e: c3 retq */
}
int main()
{
/*119f: f3 0f 1e fa endbr64
11a3: 55 push %rbp
11a4: 48 89 e5 mov %rsp,%rbp
11a7: b8 00 00 00 00 mov $0x0,%eax
11ac: e8 98 ff ff ff callq 1149 <i_c_c>*/
i_c_c();
/*11b1: b8 00 00 00 00 mov $0x0,%eax
11b6: 5d pop %rbp
11b7: c3 retq
11b8: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
11bf: 00 */
return 0;
}