1. 程式人生 > >彙編學習()

彙編學習()

1.輸入兩個數,分別作為有符號和無符號數比較大小,輸出較大值,並分別輸出差值的絕對值。

 int a;
 int b;
 int max_signed;
 unsigned int max_unsigned;
 int differ_signed_abs;
 unsigned int differ_unsigned_abs;
 
 printf("a = ");
 scanf("%d", &a);
 printf("b = ");
 scanf("%d", &b);
 _asm{
	  MOV  EAX, a
	  MOV  EBX, b
	  MOV  max_signed, EAX
	  SUB  EAX, EBX
	  JG  L1
	  //若a<=b,max值取b,且差值取絕對值
	  MOV  max_signed, EBX 
	  NEG  EAX
	L1:
	  MOV  differ_signed_abs, EAX
	  
	  MOV  EAX, a
	  MOV  EBX, b
	  MOV  max_unsigned, EAX
	  SUB  EAX, EBX
	  JA  L2 
	  //同上
	  MOV  max_unsigned, EBX 
	  NEG  EAX
	L2:
	  MOV  differ_unsigned_abs, EAX
 }
 printf("有符號數比較較大值為:\t%d\n", max_signed);
 printf("有符號數比較差的絕對值為:\t%d\n", differ_signed_abs);
 printf("無符號數比較較大值為:\t%u\n", max_unsigned);
 printf("無符號數比較差的絕對值為:\t%u\n", differ_unsigned_abs);

2.輸入四個字元,分別作為兩個字和一個雙字的資料,作為無符號數輸出。

 char s[5];
 unsigned int dw;
 unsigned int w1, w2;

 printf("s = ");
 scanf("%s", s);
 _asm{
	  LEA  EAX, s  //獲取字串儲存地址
	  MOV  EBX, w1  //獲取w1儲存地址
	  MOV  ECX, w2  //獲取w2儲存地址
	  
	  MOV  EAX, 0  
	  MOV  AX, DX  //取出低16位,即前兩個字元
	  MOV  w1, EAX
	  
	  MOV  EAX, 0
	  SHR  EDX, 16  //將原高16位右移至低16位
	  MOV  AX, DX  //取出後兩個字元
	  MOV  w2, EAX
}
printf("第一個字:\t%u\n第二個字:\t%u\n雙字:\t%u\n",w1, w2, dw);

3.獲取字串長度

 char s[81];
 int len;

 printf("s = ");
 scanf("%s", s);
 _asm{
	  LEA  EAX, s  //獲取字串儲存地址
	  MOV  ECX, -1  //初始化計數器
	L:
	  ADD  ECX, 1  
	  MOV  BL, [EAX + ECX] //取出第ECX個字元
	  CMP  BL, 0   //判定是否為'\0'
	  JNE  L
	  MOV  len, ECX
}
printf("字串的長度為:\t%d\n", len);