如何附加到字符串?
How do I append to a string?
所以我有一个字符串,我想根据用户输入向其中添加更多内容。例如,字符串的默认值为 "The two numbers from input are: $" ,一旦用户输入 2 个数字,比如 21 和 42,则字符串应更改为 "The two numbers from input are: 21 42" 以便我可以将它们写入文件。这是我的代码:
.model small
.stack 100
.data
Num1 DB ? ;input
Num2 DB ?
text db "The two numbers from input are: $" ;This is the string I want to change according to input
filename db "decimal.txt",0
handler dw ?
.code
START:
mov ax,@data
mov ds,ax
;////////////////////////////
;INPUT1
;Get tens digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
;Multiply first digit by 10 (tens)
MOV cl, al
MOV ch, 0
MOV bl, 10
MUL bl
MOV Num1, al
;Get ones digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
ADD Num1, dl
;INPUT2
;Get tens digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
;Multiply first digit by 10 (tens)
MOV cl, al
MOV ch, 0
MOV bl, 10
MUL bl
MOV Num2, al
;Get ones digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
ADD Num2, dl
;//////////////////////////
;FILE
;create file
MOV ah, 3ch
MOV cx, 0
MOV dx, offset filename
INT 21h
;file handler
MOV handler, Ax
;write string
MOV ah, 40h
MOV Bx, handler
MOV Cx, 50 ;string length
MOV Dx, offset text
INT 21h
MOV Ax, 4c00h
INT 21h
end start
有什么办法吗?我试图在互联网上搜索,但我一直得到的只是两个字符串的连接,这没有用,因为我不知道如何将我的输入更改为字符串。
一般来说,你不能在汇编程序中直接将一个数字附加到一个字符串上。你要做的是先将数字转换成字符串,然后将两个字符串连接起来。
然而,话虽如此,它在您的用例中略有不同。由于您已经知道您的两个数字都是两位数,因此您可以执行字符串格式化之类的操作 (à la printf
)。也就是说:您已经在字符串中保留了一些 space 作为数字应该去的位置并将数字写入该位置。
此外,您显然是从输入中读取数字作为字符。因此,您可以直接将这些字符存储到您的字符串中,然后再将它们转换为实际数值。
我只写下了您完成自己的实现所需的部分代码:
.data
; note the 'NN' as placeholder for the numbers to follow
text db "My numbers: NN NN", $
.code
START:
...
mov ah, 01h ; read a character
int 21h
mov ds:[text+13], al ; store the character at position 13 in the text
sub al, '0' ; convert the character to a number as before
mov cl, al
...
所以我有一个字符串,我想根据用户输入向其中添加更多内容。例如,字符串的默认值为 "The two numbers from input are: $" ,一旦用户输入 2 个数字,比如 21 和 42,则字符串应更改为 "The two numbers from input are: 21 42" 以便我可以将它们写入文件。这是我的代码:
.model small
.stack 100
.data
Num1 DB ? ;input
Num2 DB ?
text db "The two numbers from input are: $" ;This is the string I want to change according to input
filename db "decimal.txt",0
handler dw ?
.code
START:
mov ax,@data
mov ds,ax
;////////////////////////////
;INPUT1
;Get tens digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
;Multiply first digit by 10 (tens)
MOV cl, al
MOV ch, 0
MOV bl, 10
MUL bl
MOV Num1, al
;Get ones digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
ADD Num1, dl
;INPUT2
;Get tens digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
;Multiply first digit by 10 (tens)
MOV cl, al
MOV ch, 0
MOV bl, 10
MUL bl
MOV Num2, al
;Get ones digit
MOV ah, 1h
INT 21h
SUB al,30h
MOV dl, al
ADD Num2, dl
;//////////////////////////
;FILE
;create file
MOV ah, 3ch
MOV cx, 0
MOV dx, offset filename
INT 21h
;file handler
MOV handler, Ax
;write string
MOV ah, 40h
MOV Bx, handler
MOV Cx, 50 ;string length
MOV Dx, offset text
INT 21h
MOV Ax, 4c00h
INT 21h
end start
有什么办法吗?我试图在互联网上搜索,但我一直得到的只是两个字符串的连接,这没有用,因为我不知道如何将我的输入更改为字符串。
一般来说,你不能在汇编程序中直接将一个数字附加到一个字符串上。你要做的是先将数字转换成字符串,然后将两个字符串连接起来。
然而,话虽如此,它在您的用例中略有不同。由于您已经知道您的两个数字都是两位数,因此您可以执行字符串格式化之类的操作 (à la printf
)。也就是说:您已经在字符串中保留了一些 space 作为数字应该去的位置并将数字写入该位置。
此外,您显然是从输入中读取数字作为字符。因此,您可以直接将这些字符存储到您的字符串中,然后再将它们转换为实际数值。
我只写下了您完成自己的实现所需的部分代码:
.data
; note the 'NN' as placeholder for the numbers to follow
text db "My numbers: NN NN", $
.code
START:
...
mov ah, 01h ; read a character
int 21h
mov ds:[text+13], al ; store the character at position 13 in the text
sub al, '0' ; convert the character to a number as before
mov cl, al
...