Advertisement
Guest User

Untitled

a guest
Apr 14th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. asm
  2. section .text
  3.     global _sum, _do_the_job
  4.  
  5. section .data
  6.     mas_size equ 4
  7.  
  8. _do_the_job:
  9.     push ebp
  10.     mov ebp, esp
  11.  
  12.     ; eax - pointer to array start.
  13.     ; array of integers, step is 4
  14.  
  15.     mov edi, [ebp+8]
  16.     mov ebx, [ebp+12]
  17.  
  18.     ; find minimum
  19.     mov ecx, ebx
  20.     mov esi,0
  21.     mov eax,[edi + esi]
  22.    
  23.     outer:
  24.         push ecx
  25.         mov ecx, ebx
  26.     inner:
  27.         cmp eax,[edi + esi]
  28.         jle no_need
  29.         mov eax,[edi + esi]
  30.     no_need:    
  31.         add esi, 4
  32.         loop inner
  33.  
  34.         pop ecx
  35.         loop outer
  36.  
  37.     ; subtract
  38.     mov ecx, ebx
  39.     mov esi,0
  40.     _outer:
  41.         push ecx
  42.         mov ecx, ebx
  43.     _inner:
  44.         sub [edi + esi],eax
  45.        
  46.         add esi, 4
  47.         loop _inner
  48.  
  49.         pop ecx
  50.         loop _outer
  51.  
  52.     mov eax,edi
  53.     ; epilogue
  54.     mov esp, ebp
  55.     pop ebp
  56.  
  57.     ret            
  58.  
  59.  
  60. c
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <time.h>
  64.  
  65. extern void do_the_job(int* arr, int len);
  66.  
  67. #define LEN 7
  68.  
  69. void main()
  70. {
  71.     srand(time(NULL));
  72.     int* a = calloc(LEN * LEN, sizeof(int));
  73.     int i;
  74.     int j;
  75.  
  76.     for (i = 0; i < LEN; i += 1) {
  77.         for (j = 0; j < LEN; ++j) {
  78.             a[i * LEN + j] = rand() % 98 + 1;
  79.         }
  80.     }
  81.  
  82.     printf("BeFoRe:\n");
  83.  
  84.     for (i = 0; i < LEN; i += 1) {
  85.         for (j = 0; j < LEN; ++j) {
  86.             printf("%2d ", a[i * LEN + j]);
  87.         }
  88.         printf("\n");
  89.     }
  90.  
  91.     do_the_job(a, LEN);
  92.  
  93.     printf("AfTeR:\n");
  94.  
  95.     for (i = 0; i < LEN; i += 1) {
  96.         for (j = 0; j < LEN; ++j) {
  97.             printf("%2d ", a[i * LEN + j]);
  98.         }
  99.         printf("\n");
  100.     }
  101.     free(a);
  102. }
  103.  
  104.  
  105. sh
  106. clear
  107. ../nasm -f elf -o 6.o 6.asm
  108. gcc -c main.c -o main.o
  109. gcc -o main main.o 6.o
  110. ./main
  111.  
  112. run in cygwin x32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement