1. 程式人生 > 其它 >計算機組成原理上機練習13:機器碼和位運算的深入理解

計算機組成原理上機練習13:機器碼和位運算的深入理解

由於機器小段儲存的特點,按位元組輸出後排在前面的位元組是低位。要直觀理解機器碼需要首先需要把機器碼逐位元組轉為二進位制碼,然後再按小段儲存原則排開。我們做位運算時做的邏輯左移實際上在機器中是物理右移。

#include <stdio.h>
typedef unsigned char* byte_p;
int main() {
	void show_byte(byte_p var,int len);//逐個位元組輸出就是機器碼,直接print就是字面值
	int a,b,a1,b1,c;
	printf("please input two number: ");
	scanf("%d%d",&a,&b);
	printf("the machine code for %d is ",a);//小段儲存,前面存放的是低位
	show_byte((byte_p)&a,sizeof(a));
	printf("the true value for a is %d\n ",a);
	
	printf("the machine code for %d is ",b);
	show_byte((byte_p)&b,sizeof(a));
	printf("the true value for b is %d\n",b);
	
	c=a<<16;
	c=c|b;
	printf("the machine code for %d is ",c);
	show_byte((byte_p)&c,sizeof(c));
	printf("the true value for c is %d\n",c);
	
	a1=(c>>16);//右移,低位的01丟失
	b1=(c&0xffff);//取機器碼低四位(十六進位制)
	printf("the true value for a is %d\n",a1);
	printf("the true value for b is %d\n",b1);
	return 0;
}
void show_byte(byte_p var,int len)
{
	int i;
	for(i=0;i<len;i++)
		printf("%.2X ",var[i]);
	printf("\n");
}