asm
This commit is contained in:
		
							parent
							
								
									de4ee10ee2
								
							
						
					
					
						commit
						57c54c1dd3
					
				
					 3 changed files with 180 additions and 13 deletions
				
			
		
							
								
								
									
										20
									
								
								asm/idiv.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								asm/idiv.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  | 	short a, b, c, d; | ||||||
|  | 	scanf("%hd %hd", &a, &b); | ||||||
|  | 	asm | ||||||
|  | 		(			 | ||||||
|  | 		 	"mov %1,%%ax\n" | ||||||
|  | 			"mov %2,%%bx\n" | ||||||
|  | 		 	"cwd\n" | ||||||
|  | 			"idiv %%bx\n" | ||||||
|  | 			"mov %%ax, %%dx\n" | ||||||
|  | 			"mov %%dx, %0\n" | ||||||
|  | 			: "=r" (c) | ||||||
|  | 			: "r" (a), "r" (b) | ||||||
|  | 		); | ||||||
|  | 	printf("%hd\n", c); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
							
								
								
									
										133
									
								
								asm/solutions.md
									
										
									
									
									
								
							
							
						
						
									
										133
									
								
								asm/solutions.md
									
										
									
									
									
								
							|  | @ -1,46 +1,153 @@ | ||||||
| # zero | # zero | ||||||
| 
 | 
 | ||||||
| ``` | ``` | ||||||
| xor eax, eax | xor %eax, %eax | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| # one | # one | ||||||
| ``` | ``` | ||||||
| xor eax, eax | xor %eax, %eax | ||||||
| inc eax | inc %eax | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| # next | # next | ||||||
| ``` | ``` | ||||||
| inc ax | inc %ax | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| # prev | # prev | ||||||
| ``` | ``` | ||||||
| dec ax | dec %ax | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| # sum | # sum | ||||||
| ``` | ``` | ||||||
| mov dx, ax | mov %ax, %dx | ||||||
| add dx, bx | add %bx, %dx | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| # sub | # sub | ||||||
| ``` | ``` | ||||||
| mov rdx, rax | mov %rax, %rdx | ||||||
| sub rdx, rbx | sub %rbx, %rdx | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| # imul | # imul | ||||||
| ``` | ``` | ||||||
| imul bl | imul %bl | ||||||
| mov dx, ax | mov %ax, %dx | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| # idiv | # idiv | ||||||
| ``` | ``` | ||||||
| cwd | cwd | ||||||
| idiv bx | idiv %bx | ||||||
| mov dx, ax  | mov %ax, %dx  | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # lea | ||||||
|  | ``` | ||||||
|  | lea (%rax, %rbx, 8), %rdx | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # lea1 | ||||||
|  | ``` | ||||||
|  | lea (%rax, %rax, 4), %rdx | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # lea32 | ||||||
|  | ``` | ||||||
|  | lea (%eax, %ebx, 8), %edx | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # egalite | ||||||
|  | ``` | ||||||
|  | cmp %ax, %bx | ||||||
|  | jne nya | ||||||
|  | mov $1, %dx | ||||||
|  | jmp nya_ | ||||||
|  | nya: | ||||||
|  | mov $0, %dx | ||||||
|  | nya_: | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # factorial | ||||||
|  | ``` | ||||||
|  | xor %edx, %edx | ||||||
|  | inc %edx | ||||||
|  | cmp $0, %al | ||||||
|  | je end | ||||||
|  | mov %al, %cl | ||||||
|  | mov %ecx, %eax | ||||||
|  | loop: | ||||||
|  | cmp $1, %ecx | ||||||
|  | mov %eax, %edx | ||||||
|  | jle end | ||||||
|  | dec %ecx | ||||||
|  | mul %ecx | ||||||
|  | jmp loop | ||||||
|  | end: | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # factorial64 | ||||||
|  | ``` | ||||||
|  | xor %rdx, %rdx | ||||||
|  | inc %rdx | ||||||
|  | cmp $0, %al | ||||||
|  | je end | ||||||
|  | mov %al, %cl | ||||||
|  | mov %rcx, %rax | ||||||
|  | loop: | ||||||
|  | cmp $1, %rcx | ||||||
|  | mov %rax, %rdx | ||||||
|  | jle end | ||||||
|  | dec %rcx | ||||||
|  | mul %rcx | ||||||
|  | jmp loop | ||||||
|  | end: | ||||||
|  | ``` | ||||||
|  | # antifa | ||||||
|  | ``` | ||||||
|  | xor %ecx, %ecx | ||||||
|  | inc %ecx | ||||||
|  | loop: | ||||||
|  | cmp $1, %eax | ||||||
|  | je end | ||||||
|  | inc %ecx | ||||||
|  | cdq | ||||||
|  | div %ecx | ||||||
|  | jmp loop | ||||||
|  | end: | ||||||
|  | mov %ecx, %ebx | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # antifa64 | ||||||
|  | ``` | ||||||
|  | xor %rcx, %rcx | ||||||
|  | inc %rcx | ||||||
|  | loop: | ||||||
|  | cmp $1, %rax | ||||||
|  | je end | ||||||
|  | inc %rcx | ||||||
|  | cqo | ||||||
|  | div %rcx | ||||||
|  | jmp loop | ||||||
|  | end: | ||||||
|  | mov %rcx, %rbx | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | # vector | ||||||
|  | ``` | ||||||
|  | xor %rbx, %rbx | ||||||
|  | cmp $0, %rcx | ||||||
|  | je end | ||||||
|  | dec %rcx | ||||||
|  | loop: | ||||||
|  | lea (%rax, %rcx, 8), %rdx | ||||||
|  | add (%rdx), %rbx | ||||||
|  | dec %rcx | ||||||
|  | cmp $-1, %rcx | ||||||
|  | je end | ||||||
|  | jmp loop | ||||||
|  | end: | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
							
								
								
									
										40
									
								
								asm/vector.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								asm/vector.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,40 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdint.h> | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  | 	//uint64_t c = 5;
 | ||||||
|  | 	uint64_t b, c = 5; | ||||||
|  | 	int64_t *a; | ||||||
|  | 	int64_t array[] = {1, 2, 3, 4, 5}; | ||||||
|  | 	a = array; | ||||||
|  | 	//printf("pointer: %p\n", a);
 | ||||||
|  | 	asm ( | ||||||
|  | 		"mov %1, %%rax\n" | ||||||
|  | 		"mov %2, %%rcx\n" | ||||||
|  | 
 | ||||||
|  | 		"xor %%rbx, %%rbx\n" | ||||||
|  | 		"cmp $0, %%rcx\n" | ||||||
|  | 		"je end\n" | ||||||
|  | 		 | ||||||
|  | 		"dec %%rcx\n" | ||||||
|  | 
 | ||||||
|  | 		"loop:\n" | ||||||
|  | 
 | ||||||
|  | 		"lea (%%rax, %%rcx, 8), %%rdx\n" | ||||||
|  | 		"add (%%rdx), %%rbx\n" | ||||||
|  | 		"dec %%rcx\n" | ||||||
|  | 
 | ||||||
|  | 		"cmp $-1, %%rcx\n" | ||||||
|  | 		"je end\n" | ||||||
|  | 		"jmp loop\n" | ||||||
|  | 
 | ||||||
|  | 		"end:\n" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 		"mov %%rbx, %0\n" | ||||||
|  | 		: "=r" (b) | ||||||
|  | 		: "r" (a), "r" (c) | ||||||
|  | 	    ); | ||||||
|  | 	printf("%lld\n", b); | ||||||
|  | 
 | ||||||
|  | } | ||||||
		Reference in a new issue