我正在尝试创建一个x86函数,它接受两个字符串作为参数,并确定哪个更大。参数是通过用户输入获得的,我正在使用NASM。然而,一旦我开始工作,我就会在c程序中调用我的汇编函数,这样可能只会给自己提供比测试所需的更多的工作。我真的很感激任何帮助,找出为什么这个代码错误。
SECTION .data
greet: db "Type a word: "
greetL: equ $-greet
bigprompt: db "First is bigger"
bigpromptL: equ $-bigprompt
smallprompt: db "Second is bigger"
smallpromptL: equ $-smallprompt
sameprompt: db "They same: "
samepromptL: equ $-sameprompt
LF: equ 10
SECTION .bss
first: resb 30
second: resb 30
SECTION .text
global _start
_start:
nop
mov eax, 4 ; ask for first word
mov ebx, 1
mov ecx, greet
mov edx, greetL
int 80H
mov eax, 3 ; get first word
mov ebx, 0
mov ecx, first
mov edx, 30
int 80H
mov eax, 4 ; ask for second word
mov ebx, 1
mov ecx, greet
mov edx, greetL
int 80H
mov eax, 3 ; get second word
mov ebx, 0
mov ecx, second
mov edx, 30
int 80H
push first
push second
call _Stringgt
mov esi, eax
jmp DONE
_Stringgt:
push ebp
mov ebp, esp
push edx
mov eax, [ebp+8]
mov ebx, [ebp+12]
cld
xor ecx, ecx
LOOP:
mov al, [eax + ecx]
cmp [ebx + ecx], al
jb BIGGER
ja SMALLER
cmp al, LF
je SAME
inc ecx
jmp LOOP
BACK:
pop ebp
ret
BIGGER:
mov eax, bigprompt
jmp BACK
SMALLER:
mov eax, smallprompt
jmp BACK
SAME:
mov eax, sameprompt
jmp BACK
DONE:
mov eax, 4
mov ebx, 1
mov ecx, esi
mov edx, 30
int 80H
mov eax, 1 ; exit
mov ebx, 0
int 80H
答案 0 :(得分:0)
这太简单了。您可以使用此函数来获取字符串的长度并确定哪个更大。
;---------------------------------------------------------
;IN: takes zero terminated string address as parameter on stack
;OUT: String length in AX
;---------------------------------------------------------
str_length:
mov bp,sp
mov si, [bp+2]
mov al, [si]
mov bx,0
.loop:
cmp byte [si], 0
je .done
add si,1
add bx,1
jmp .loop
.done:
mov ax,bx
ret
;--------------------------------------------------------------
现在你可以做两次str_length并比较字符串长度,比如
main:
mov ax,string1
push ax ;give function parameter
call str_length ;call the function
mov dx,ax ;store string length in dx
mov ax,string2
push ax
call str_length ; do this for string2
;now length of string1 is in dx and string2 is in ax we can compare them
cmp ax,dx
je equal
cmp ax,dx
ja greater
cmp ax,dx
jl less