Experiment a compilation of basic programming exercises

The statement on my blog

1. Write a program, complete the calculation of the following formula, and outputs the result

								A ←(X-Y+24)/ Z 的商,B ← (X-Y+24)/ Z 的余数
		其中,变量X,Y是32位有符号数,变量A,B,Z是16位有符号数。

Directly on the code, which has commented code

#include<stdio.h>

int x,y,tmp;  
short int a,b,z;

int main()
{
	printf("请按顺序输入x,y,z:\n");
	scanf("%d%d%d",&x,&y,&z);

	_asm{
		mov eax,x   ;把x给eax寄存器
		sub eax,y   ;执行x-y
		add eax,24		;x-y+24
		mov edx,eax   ;为了idiv准备
		shr edx,16     ;将高16位移到低十六位
		idiv z          ;用dx:ax  除以z (z是16位)  dx:ax   /   z   =  ax.......dx  (32位除以16位的模板)
		mov a,ax   ;把商移入a
		mov b,dx	 ;余数移入b
	}
	//printf("%d\n",tmp);
	printf("计算结果为:\n商:%d\n余数:%d\n",a,b);

	return 0;
}

2. X data word split into four nibbles of data are sequentially stored in the unit thereof

#include<stdio.h>

short int x;
short int a,b,c,d;

int main()
{
	printf("请输入一个字数据:\n");
	scanf("%d",&x);
	//x=0x1234; 十进制:x=4660    (0x代表16进制)
	_asm
	{
		mov ax,x  ;x是十六位
		and ax,0fh  ;只保留低四位
		mov a, ax  ; a=4
		
		mov ax,x 
		and ax,0f0h  ;保留al中的高四位
		shr ax,4  ;逻辑右移四位
		mov b, ax  ;b=3

		mov ax,x 
		and ax,0f00h
		shr ax,8
		mov c, ax ;c=2

		mov ax,x 
		and ax,0f000h
		shr ax,12
		mov d, ax  ;d=1
	}
	
	printf("A=%d\nB=%d\nC=%d\nD=%d\n",a,b,c,d);

	return 0;
}

Receiving from the keyboard 3. two decimal digits no greater than 5, and displays the data and in decimal form.

Do not enter two 5 in order to avoid the situation can not be directly output occurs.

#include<stdio.h>
short x,y;
int sum;
int main()
{
	printf("please input two integer that is less than five:");
	scanf("%d%d",&x,&y);

	_asm{
	mov ax,x
	add ax,y
	mov sum,eax
	}

	printf("the sum of %d and %d is: %d\n",x,y,sum);

return 0;
}

4. receiving a character string (assuming that the input character string length greater than 3) from the keyboard, the output of the test line feed character string in the second consecutive characters starting 2

#include<stdio.h>

char s[100];
char a,b;

int main()
{	
	printf("please input a string that is more than three :\n");
	scanf("%s",s);

	_asm
	{
		lea ecx,s   ;取首地址
		mov al,[ecx+1]
		mov ah,[ecx+2]
		mov a,al
		mov b,ah
	}

	printf("the char is : %c and %c \n",a,b);

return 0;
}
Published 29 original articles · won praise 13 · views 2769

Guess you like

Origin blog.csdn.net/zmx2473162621/article/details/102991344