Advertisement
ra1n

x86 Inline Assembly Array Loop

Jan 28th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char **argv)
  4. {
  5.     int numbers[10];
  6.     int index = 0;
  7.  
  8.     char *array_sum = "The total sum of array is: %d";
  9.     char *array_elements = "Numbers - %d\n";
  10.  
  11.     __asm
  12.     {
  13.         $CHECK_index:
  14.             // Move index value to eax register
  15.             mov ecx, index
  16.             // Check if index >= 10
  17.             cmp ecx, 10
  18.             // Jump to $END_LOOP if true
  19.             jge $END_LOOP
  20.             // Move value of ecx register to array
  21.             mov numbers[TYPE numbers * ecx], ecx
  22.             // Increment the value of index by 1
  23.             inc index
  24.             // Jump to start of check loop
  25.             jmp $CHECK_index
  26.  
  27.         $END_LOOP:
  28.             // Assign value of 0 to index
  29.             mov index, 0
  30.             // Assign value of 0 to ebx register
  31.             mov ebx, 0
  32.             // Jump to $PRINT_numbers loop
  33.             jmp $PRINT_numbers
  34.  
  35.         $PRINT_numbers:
  36.             // Move index value to eax register
  37.             mov ecx, index
  38.             // Check if index >= 10
  39.             cmp ecx, 10
  40.             // Jump to $TERMINATE if true
  41.             jge $TERMINATE
  42.             // Move array value into eax register
  43.             mov eax, numbers[TYPE numbers * ecx]
  44.             // Sum the values in the ebx register
  45.             add ebx, eax
  46.             // Push eax to the stack
  47.             push eax
  48.             // Push formatted string to stack
  49.             push array_elements
  50.             // Call printf
  51.             call printf
  52.             // Increment the value of index by 1
  53.             inc index
  54.             // Jump to start of print numbers loop
  55.             jmp $PRINT_numbers
  56.  
  57.         $TERMINATE:
  58.             // Push ebx to the stack
  59.             push ebx
  60.             // Push formatted string to stack
  61.             push array_sum
  62.             // Call printf
  63.             call printf
  64.             // Clean the stack
  65.             add esp, 16
  66.     }
  67.    
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement