📄 stricmp.c
字号:
#include <ctype.h>#include "as31glue.h"#include "stricmp.h"xdata char * data strcpy_asm_src_addr;#pragma LESS_PEDANTIC// Fast stricmp, returns -1 if *a < *b, 0 if equal, +1 if *a > *b// Call this as:// stricmp(xdata char *a, xdata char *b)char stricmp_asm(xdata char *a) _naked{ a; _asm inc AUXR1 // switch to DPTR1 & load w/ "b" mov dpl, _strcpy_asm_src_addr+0 mov dph, _strcpy_asm_src_addr+1 inc AUXR1 // switch to "a" pointerstricmp_loop: inc AUXR1 // switch to "b" pointer movx a, @dptr // fetch byte from "b" string inc dptr cjne a, #97, stricmp_b_1stricmp_b_1: jc stricmp_b cjne a, #123, stricmp_b_2stricmp_b_2: jnc stricmp_b add a, #224stricmp_b: mov b, a // keep uppercase of *b in b register inc AUXR1 // switch to "a" pointer movx a, @dptr // fetch byte from "a" string inc dptr cjne a, #97, stricmp_a_1stricmp_a_1: jc stricmp_a cjne a, #123, stricmp_a_2stricmp_a_2: jnc stricmp_a add a, #224stricmp_a: // acc has uppercase of *a cjne a, b, stricmp_a_ne_b jnz stricmp_loopstricmp_a_eq_b: mov dpl, #0 retstricmp_a_ne_b: // not equal, C bit tells which was greater jnc stricmp_a_gt_bstricmp_a_lt_b: mov dpl, #-1 retstricmp_a_gt_b: mov dpl, #1 ret _endasm;}// Fast memset. Fills a block of memory with a particular byte.// Call this as:// memset(xdata void *ptr, unsigned char ch, unsigned char length)void memset_asm(xdata void *ptr) _naked{ ptr; _asm mov b, r0 mov r0, _strcpy_asm_src_addr mov a, (_strcpy_asm_src_addr + 1)memset_loop: movx @dptr, a inc dptr djnz r0, memset_loop mov r0, b ret _endasm;}// Fast string initialization. Fills a string with a character// and put a null termination on the end.// Call this as:// string_init(xdata char *str, char ch, unsigned char sizeof(str));void string_init_asm(xdata char *str) _naked{ str; _asm lcall _memset_asm clr a movx @dptr, a ret _endasm;}// copy string, but don't copy more than len-1 bytes and always// null terminate (note, this one always gives a null termination// on the end, unlike the "standard" strncmp in many C libraries)void strncpy_asm(xdata char *dest_addr, xdata unsigned char len) _naked{ dest_addr; // passed in DPTR len; // passed @ _strncpy_asm_PARM_2 _asm inc AUXR1 mov dptr, #_strncpy_asm_PARM_2 movx a, @dptr dec a mov b, a sjmp fast_strcpy_entry _endasm;}void strcpy_asm(xdata char *dest_addr){ dest_addr; // supress compiler's unused variable warning _asm // on entry, DPTR0 has destination addr inc AUXR1 // switch to DPTR1 & load w/ src addr mov b, #255fast_strcpy_entry: mov dpl, _strcpy_asm_src_addr+0 mov dph, _strcpy_asm_src_addr+1fast_strcpy_loop: movx a, @dptr // read source jz fast_strcpy_done inc dptr inc AUXR1 // switch to DPTR0 (dest_addr) movx @dptr, a // write destination inc dptr inc AUXR1 // switch to DPTR1 (src_addr) djnz b, fast_strcpy_loopfast_strcpy_done: inc AUXR1 // switch to DPTR0 (dest_addr) clr a movx @dptr, a // don't forget to null-terminate the copy _endasm;}unsigned int strlen_asm(xdata char *str){ str; _asm inc AUXR1 mov dptr, #0 inc AUXR1strlen_asm_loop: movx a, @dptr jz strlen_asm_end inc dptr inc AUXR1 inc dptr inc AUXR1 sjmp strlen_asm_loopstrlen_asm_end: inc AUXR1 _endasm;}unsigned char str_is_dot_mp3(xdata unsigned char *str){ str; _asm movx a, @dptr // fetch first byte cjne a, #0x2E, str_is_not_dot_mp3 // check '.' inc dptr movx a, @dptr // fetch second byte orl a, #0x20 cjne a, #0x6D, str_is_not_dot_mp3 // check 'm' inc dptr movx a, @dptr // fetch third byte orl a, #0x20 cjne a, #0x70, str_is_not_dot_mp3 // check 'p' inc dptr movx a, @dptr // fetch fourth byte cjne a, #0x33, str_is_not_dot_mp3 // check '3' inc dptr movx a, @dptr // fetch last byte jnz str_is_not_dot_mp3 mov dpl, #1 retstr_is_not_dot_mp3: mov dpl, #0 ret _endasm;}unsigned char str_is_dot_m3u(xdata unsigned char *str){ str; _asm movx a, @dptr // fetch first byte cjne a, #0x2E, str_is_not_dot_m3u // check '.' inc dptr movx a, @dptr // fetch second byte orl a, #0x20 cjne a, #0x6D, str_is_not_dot_m3u // check 'm' inc dptr movx a, @dptr // fetch third byte orl a, #0x20 cjne a, #0x33, str_is_not_dot_m3u // check '3' inc dptr movx a, @dptr // fetch fourth byte cjne a, #0x75, str_is_not_dot_m3u // check 'u' inc dptr movx a, @dptr // fetch last byte jnz str_is_not_dot_m3u mov dpl, #1 retstr_is_not_dot_m3u: mov dpl, #0 ret _endasm;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -