/* * Testing inline assembler with HI-TECH C under CP/M */ #include void fnctpar(unsigned int); void regvar1(); void regvar2(); void delay(long); /* Entry point to program */ int main() { unsigned int i; char c; printf("Hello world !\n"); /* Set SC103 - Z80 PIO's to output mode */ #asm PUSH AF LD A, 0FH OUT (6AH), A OUT (6BH), A OUT (6EH), A OUT (6FH), A POP AF #endasm printf("======================================\n"); printf("Getting function parameter to register\n"); fnctpar(170); printf("--------------------------------------\n"); printf("Press Enter to continue..."); scanf("%c", &c, 1); fnctpar(85); printf("======================================\n"); printf("Press Enter to continue..."); scanf("%c", &c, 1); fnctpar(3855); printf("--------------------------------------\n"); printf("Press Enter to continue..."); scanf("%c", &c, 1); printf("======================================\n"); /* This page says: www.vcfed.org/forum/archive/index.php/t-50530.html If there is a register variable (there is at most one) it's in register IY L.V.: Note this is NOT TRUE, variable declared as 'register' is accessible via IX-displacement as other local variables ! 'register' it is just a hint to the compiler. It is up to the latter to satisfy your desire or not. */ printf("Getting register-variable to register pair - OPTIMALIZED\n"); regvar1(); printf("--------------------------------------\n"); printf("Getting register-variable to register pair - NOT OPTIMALIZED\n"); regvar2(); printf("Press Enter to continue..."); scanf("%c", &c, 1); printf("======================================\n"); printf("Getting local variable to register pair\n"); for (i = 0; i < 65535; i++) { #asm PUSH AF PUSH HL /* Local variables can be accessed by IX-displacement */ LD H, (IX-1) ; Pick up the high-byte of local 'i' LD L, (IX-2) ; Pick up the low-byte of local 'i' LD A, L OUT (00H), A OUT (068H), A CPL OUT (06CH), A LD A, H OUT (069H), A CPL OUT (06DH), A POP HL POP AF #endasm delay(500l); if (i % 256 == 0) printf("\ni: %u\n", i); else printf("."); } printf("\n"); return 0; } /* Getting register-variable to register pair HL */ /* Expect, that compiler optimalized storing 'register unsigned int ri' to IY */ void regvar1() { register unsigned int ri = 3855; /* 0000111100001111 */ unsigned int iy_copy; #asm PUSH AF PUSH HL DEFB 0FDH, 7DH ; LD A, IYL (Undocumented) OUT (00H), A OUT (68H), A OUT (6CH), A LD L, A DEFB 0FDH, 7CH ; LD A, IYH (Undocumented) OUT (69H), A OUT (6DH), A LD H, A /* Local variables can be accessed by IX-displacement */ /* Because we expect that 'ri' is in IY, iy_copy is at IX-1, IX-2 */ LD (IX-1), H ; Put the high-byte to local 'iy_copy' LD (IX-2), L ; Put the low-byte to local 'iy_copy' POP HL POP AF #endasm /* This comparsion fails */ printf("ri: %d, iy_copy: %d\n", ri, iy_copy); /* Assure that ri and iy_copy have the same value */ printf("ri: %4.4x, iy_copy: %4.4x\n", ri, iy_copy); /* Assure that ri and iy_copy have the same value */ } /* Getting register-variable to register pair HL */ /* Expect, that compiler DO-NOT optimalized storing 'register unsigned int ri' to IY */ void regvar2() { register unsigned int ri = 3855; /* 0000111100001111 */ unsigned int li = 0; #asm PUSH AF PUSH HL /* Local variables can be accessed by IX-displacement */ LD H, (IX-1) ; Pick up the high-byte of reg. local 'ri' LD L, (IX-2) ; Pick up the low-byte of reg. local 'ri' LD A, L OUT (00H), A OUT (68H), A OUT (6CH), A LD A, H OUT (69H), A OUT (6DH), A LD (IX-3), H ; Put the high-byte to local 'li' LD (IX-4), L ; Put the low-byte to local 'li' POP HL POP AF #endasm /* This comparsion success */ printf("ri: %d, li: %d\n", ri, li); /* Assure that ri and li have the same value */ printf("ri: %4.4x, li: %4.4x\n", ri, li); /* Assure that ri and li have the same value */ } /* Getting function parameter to register */ void fnctpar(unsigned int fi) { unsigned int li = 0; #asm PUSH AF PUSH HL /* Function parameters can be accessed by IX+displacement */ /* The first argument is located at (IX+6) */ LD H, (IX+7) ; Pick up the high-byte of parameter 'fi' LD L, (IX+6) ; Pick up the low-byte of parameter 'fi' LD A, L OUT (00H), A OUT (68H), A OUT (6CH), A LD A, H OUT (69H), A OUT (6DH), A LD (IX-1), H ; Put the high-byte to local 'li' LD (IX-2), L ; Put the low-byte to local 'li' POP HL POP AF #endasm printf("fi: %d, li: %d\n", fi, li); /* Assure that fi and li have the same value */ printf("fi: %4.4x, li: %4.4x\n", fi, li); /* Assure that fi and li have the same value */ } void delay(long duration) { long i; for (i = 0; i < duration; i++) { ; #asm NOP #endasm } }