如何在emu8086中使用字符串 [英] How to use strings in emu8086
本文介绍了如何在emu8086中使用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要emu8086中的字符串帮助.我已经初始化了一个字符串:
I need help with strings in emu8086. I have initialized a string:
str1 db "0neWord"
我有一个空字符串:
str2 db ?
现在我需要检查str1
中的所有字母并将其复制到str2
,但是如果str1
中的字母为0,则需要将其替换为O.否则,我只需要复制字母即可
Now I need to check all letters in str1
and copy to str2
, but if the letter in str1
is 0, I need to replace it with O. If not, I need to just copy the letter.
我该怎么做?
推荐答案
有多种方法可以做到这一点.以下是一些示例:
There are multiple ways to do that. Here are some examples:
1)具有字符串指令:
.model small
.data
str1 db "0neWord$"
size equ $-str1
str2 db size dup ('')
.code
main:
mov ax, @data
mov ds, ax
mov cx, size
cld ; DF might have been set, but we want lodsb to go forwards
lea si, str1
mov ax, 0
mov bx, 0
copyStr:
lodsb ;str1 to al
cmp al, '0'
je alterChar
mov str2[bx], al
jmp continue
alterChar:
mov str2[bx], 'o'
continue:
inc bx
loop copyStr
mov str2[bx], '$'
mov ah, 09h
lea dx, str2
int 21h
mov ah, 04ch
int 21h
end main
2)没有字符串指令:
.model small
.data
str1 db "0neWord$"
str2 db ?
.code
main:
mov ax, @data
mov ds, ax
mov si, 0
call copyStr
mov ah, 09h
lea dx, str2
int 21h
mov ah, 04ch
int 21h
copyStr proc
mov bx, 0
compute:
mov bl, str1 [si]
cmp bl, '0'
je alterChar
mov str2[si], bl
jmp continue
alterChar:
mov str2 [si], 'o'
continue:
inc si
cmp str1[si], '$'
je return
jmp compute
return:
mov str2[si], '$'
ret
copyStr endp
end main
LODSB Instruction you can learn more about string instructions from here
3)使用lodsb/stosb并进行了简化/优化:
.model small
.data
str1 db "0neWord$"
size equ $-str1
str2 db size dup ('')
.code
main:
mov ax, @data
mov ds, ax
mov es, ax ; stosb stores to [es:di]
mov si, OFFSET str1
mov di, OFFSET str2
cld ; make sure stosb/lodsb go forwards
; copy SI to DI, including the terminating '$'
copyStr: ; do {
lodsb ; str1 to al
cmp al, '0'
je alterChar
doneAlteration:
stosb ; al to str2
cmp al, '$'
jne copyStr ; } while(c != '$')
mov ah, 09h ; print implicit-length string
mov dx, OFFSET str2
int 21h
mov ah, 04ch ; exit
int 21h
alterChar:
mov al, 'o'
;jmp doneAlteration
stosb
jmp copyStr
end main
这篇关于如何在emu8086中使用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文