Advertisement
MichaelPetch

SO 78383750

Apr 25th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MINGW_ASM
  3.  
  4. // compile with:
  5. // gcc -masm=intel -m32 file.c
  6.  
  7. int main() {
  8. unsigned char feld[5]; // Initialize the array with 0
  9. unsigned char* p_feld = feld; // Set the pointer to the first element of the array
  10. unsigned short int i;
  11.  
  12. #ifdef MINGW_ASM
  13. printf("Executing assembler code.\n"); // Corrected quotation marks
  14. asm (
  15. "mov eax, 4;" // Set the starting index for the loop
  16. "mov cl, 5;" // Initialize with the value 5
  17. "loop: mov [eax + %[p_feld]], cl;" // Store 'cl' in the corresponding array element
  18. "add cl, -1;" // Decrement 'cl'
  19. "add eax, -1;" // Move to the previous element
  20. "jge loop;" // Jump back as long as 'cl' >= 0
  21. :
  22. : [p_feld] "r" (p_feld) // Update 'p_feld' as an output operand
  23. : "eax", "cl", "memory" // Registers altered by the assembler
  24. // memory clobber tells compiler that memory it
  25. // isn't aware of is being accessed in the assembly
  26. );
  27. #else
  28. printf("Assembler code will not be executed.\n");
  29. #endif
  30.  
  31. // Output the array to see if it contains the expected values
  32. for (i = 0; i < 5; i++) {
  33. printf("feld[%d] = %d\n", i, feld[i]);
  34. }
  35.  
  36. return 0; // Successful program termination
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement