/* ================================================================ ** File: ltoa.c ** Author: Robert J. (AntiSense) ** Date: 13.05.2000 ** Status: FREE! ** */ #include "ltoa.h" // ---------------------------------------------------------------- // Function: Normal // Purpose: Simple copy of the converted number into // the OutString buffer. // // Example: n = 0x1000 // OutString[]="0000004096"; // // n = 0x1000 // flags = LTOA_LIMITER|7<<8 // OutString[]="0004096"; // asm void LTOA_Normal(char *destbuf, unsigned long n, unsigned short flags) { .set reorder addiu sp, sp ,-4 sw ra, 0(sp) move t7, a0 // save the destination strbuf move a0, a1 // get nuber to convert jal LTOA_Convert nop la t6, asciinumb // preload asciinumber andi at, a2, LTOA_CUTZEROS beq at, $0, xan_tstl li t5, 10 // skip leading zeros li t4, 48 addiu t8, t6, 10 // t8 = eptr of asciinumber xacz_2: lbu t3, 0(t6) // skip zeros '0' loop addiu t6, t6, 1 bne t3, t4, xacz_3 addiu t5, t5, -1 bgtz t5, xacz_2 xacz_3: addiu t6, t6, -1 // get previous ptr xacz_4: lbu t0, 0(t6) // copy remaining addiu t6, t6, 1 // numbers sb t0, 0(t7) // into OutString addiu t7, t7, 1 bne t6, t8, xacz_4 b xan_x xan_tstl: andi at, a2, LTOA_LIMITER // test flag bne at, $0, xan_2 // 1 means set limiter xan_ign: la t6, asciinumb // simple copy of the asciinumber lbu t0, 0(t6) lbu t1, 1(t6) lbu t2, 2(t6) lbu t3, 3(t6) lbu t4, 4(t6) lbu t5, 5(t6) lbu t8, 6(t6) lbu t9, 7(t6) sb t0, 0(t7) sb t1, 1(t7) sb t2, 2(t7) lbu t0, 8(t6) sb t3, 3(t7) lbu t1, 9(t6) sb t4, 4(t7) sb t5, 5(t7) sb t8, 6(t7) sb t9, 7(t7) sb t0, 8(t7) sb t1, 9(t7) addiu t7, t7, 10 b xan_x xan_2: srl a3, a2, 8 // get ammount andi a3, a3, 255 // mask beq a3, $0, xan_ign // check bounds sltiu at, a3, 10 // 1 < n < 10 beq at, $0, xan_ign la a2, asciinumb+10 // converted asciinumber subu a2, a2, a3 // endpointer - ammount xan_3: // copy limited lbu t0, 0(a2) // numbers into outstring addiu a2, a2, 1 sb t0, 0(t7) addiu t7, t7, 1 addiu a3, a3, -1 bgtz a3, xan_3 xan_x: sb $0, 0(t7) // set terminator lw ra, 0(sp) addiu sp, sp, 4 jr ra } // ---------------------------------------------------------------- // Function: Formated // Purpose: Formated Number output with the option to cut // the leading zeros. // The seperator defines the char which is to be used. // Like: 1.024 or 1-024 // asm void LTOA_Formated(char *destbuf, unsigned long n, unsigned short flags, char seperator) { .set reorder addiu sp, sp ,-4 sw ra, 0(sp) move t7, a0 move a0, a1 move v1, a3 jal LTOA_Convert nop la t6, asciinumb // t6 = pointer of ascii_number addiu t8, t6, 10 // t8 = endpointer of ascii_number andi at, a2, LTOA_CUTZEROS // if flag LTOA_CUTZEROS is set beq at, $0, xafc // update t6 li t5, 10 // skip leading zeros li t4, 48 xafl0: lbu t3, 0(t6) // skip zeros '0' loop addiu t6, t6, 1 bne t3, t4, xafl1 addiu t5, t5, -1 bgtz t5, xafl0 xafl1: addiu t6, t6, -1 // get previous ptr xafc: subu t9, t8, t6 // calculate the ammout andi at, a2, LTOA_LIMITER beq at, $0, xaf_nolimit andi at, a2, LTOA_CUTZEROS bne at, $0, xaf_nolimit srl a3, a2, 8 andi a3, a3, 255 beq a3, $0, xaf_nolimit // check bounds sltiu at, a3, 10 // 1 < n < 10 beq at, $0, xaf_nolimit move t9, a3 // store limit ammount to t9 subu t6, t8, t9 // adjust pointer to limited count xaf_nolimit: li t0, 0 sltiu at, t9, 4 // if ammount < 4 then bne at, $0, xan_noli // no extra byte needed li t0, 1 // if ammount < 7 then sltiu at, t9, 7 // one extra byte needed bne at, $0, xan_noli li t0, 2 // if ammout < 10 then sltiu at, t9, 10 // two extra by needed bne at, $0, xan_noli li t0, 3 xan_noli: addu t7, t7, t0 addu t7, t7, t9 // this time copy backward sb $0, 0(t7) // set terminator addiu t7, t7, -1 la t9, asciinumb+9 // get eptr addiu t6, t6, -1 // thats for the for loop // it says that we copy the whole // number xan_format: li t4, 3 xan_for0: lbu t0, 0(t9) // copy three bytes addiu t9, t9, -1 sb t0, 0(t7) addiu t7, t7, -1 beq t9, t6, xan_fx // end reached => exit addiu t4, t4, -1 bgtz t4, xan_for0 sb v1, 0(t7) // set seperator addiu t7, t7, -1 b xan_format xan_fx: lw ra, 0(sp) addiu sp, sp, 4 jr ra } // ---------------------------------------------------------------- // Function: Time // Purpose: The parameter n indicates the frames. // The frames are divided by the paramter framerate to // evaluate the time. // The time string looks like: // 00:00:12 or if you define '.' as seperator then // 00.00.00 // asm void LTOA_Time(char *destbuf, unsigned long n, unsigned short flags, unsigned short framerate, char seperator) { .set reorder lw v1, 16(sp) // get seperator addiu sp, sp ,-4 sw ra, 0(sp) move t7, a0 move a0, a1 divu a0, a3 // divide frames by framerate mflo a0 // get seconds la t6, asciinumb li t0, 3600 divu a0, t0 mfhi a3 // get the qutient mflo a0 // get rem (Hours) andi at, a2, LTOA_TIME_H beq at, $0, tnoh jal LTOA_Convert nop lbu t0, 8(t6) // copy hours lbu t1, 9(t6) sb t0, 0(t7) sb t1, 1(t7) addiu t7, t7, 2 tnoh: li t0, 60 divu a3, t0 mfhi a3 mflo a0 andi at, a2, LTOA_TIME_M beq at, $0, tnom andi at, a2, LTOA_TIME_H beq at, $0, tnoh1 sb v1, 0(t7) // set seperator addiu t7, t7, 1 // adjust by ptr by 3 tnoh1: jal LTOA_Convert nop lbu t0, 8(t6) lbu t1, 9(t6) sb t0, 0(t7) sb t1, 1(t7) addiu t7, t7, 2 tnom: move a0, a3 andi at, a2, LTOA_TIME_S beq at, $0, tnos andi at, a2, LTOA_TIME_M beq at, $0, tnom1 sb v1, 0(t7) // set seperator addiu t7, t7, 1 // adjust ptr by 3 tnom1: jal LTOA_Convert nop lbu t0, 8(t6) lbu t1, 9(t6) sb t0, 0(t7) sb t1, 1(t7) addiu t7, t7, 2 tnos: sb $0, 0(t7) // set terminator lw ra, 0(sp) addiu sp, sp, 4 jr ra } // ---------------------------------------------------------------- // Function: Convert // Purpose: Convert the unsigned long passed as parameter // into the private AscII-Number buffer. // // USES: t0, t1, t2, t3, t4, t5, a0 // asm void LTOA_Convert(unsigned long n) { .set reorder la t0, pot la t1, asciinumb li t2, 10 li t4, 48 xac_l0: lw t3, 0(t0) addiu t2, t2, -1 addiu t0, t0, 4 divu a0, t3 mfhi a0 mflo t5 addu t5, t5, t4 sb t5, 0(t1) addiu t1, t1, 1 bgtz t2, xac_l0 jr ra }