刚学了两天的汇编非常的菜,做了个两个等长字符串的比较程序,用C语言混编的,由于TASM不支持SIZEOF伪指令,我不知道如何获得用户输入字符串的长度,想做一个变长的字符串的比较程序,请各位高手指点,谢谢啦。
下面贴上源代码:
; t1.asm
PUBLIC _strcmp
.model small
.186
.code
_strcmp PROC C s1:BYTE, s2:BYTE, c:WORD
push bx ; save registers
push si
push di
mov si,OFFSET s1 ; address to s1
mov di,OFFSET s2 ; address to s2
mov cx,c ; set counter to c
L:
mov ah, [si] ; indirect access
mov bh, [di]
cmp ah,bh ; compare two character
jne RES1 ; if not equal
inc si ; move pointer by 1
inc di
loop L ; loop the label
mov ax,1 ; two strings are completely equal
pop di ; restore registers
pop si
pop bx
ret
RES1:
mov ax,0 ; no equal
pop di ; restore registers
pop si
pop bx
ret
_strcmp ENDP
END
/* test1.cpp */
#include <stdio.h>
extern "C" int strcmp(char *s1, char *s2, int size);
int main() {
char *s1 = "Hello", *s2 = "Hello";
printf("%s\n",strcmp(s1,s2,5) ? "Equal" : "Not Equal");
return 0;
}
编译方法:
打开BC或者TC的IDE,创建一个PROJECT,将这两个文件添加进去,然后RUN就可以了。