获取输入字符串的代码
在Windows机器上使用nasm进行汇编:nasm file.asm -o file.com
;read the string
mov ah,0x0A ;read
mov dx,buffer ;pointer to buffer
int 0x21
;newline
mov ah,2
mov dl,10
int 0x21
mov dl,13
int 0x21
;put $ sign at end of string
mov bx,buffer+1
mov dx,buffer+2
add dl,byte[bx]
mov bx,dx
mov byte[bx],'$'
;output
mov dx,buffer+2
mov ah,9
int 0x21
;exit
mov ah,0x4c
int 0x21
;buffer
buffer:
db 255 ;len of buffer
db 0 ;num of char read
db 255 ;actual string
;############################
它是长度为2个字符串的字符串的工作文件,之后它发出垃圾。
答案 0 :(得分:5)
这是应该如何解决的。查看;;
的行。
bits 16 ;; you have to tell NASM to generate 16-bit code
org 0x100 ;; you have to start generating addresses at 0x100 for .COM programs
;read the string
mov ah,0x0A ;read
mov dx,buffer ;pointer to buffer
int 0x21
;newline
mov ah,2
mov dl,10
int 0x21
mov dl,13
int 0x21
;put $ sign at end of string
mov bx,buffer+1
mov dx,buffer+2
add dl,byte[bx]
adc dh, 0 ;; we're doing 16-bit+8-bit=16-bit addition
mov bx,dx
mov byte[bx],'$'
;output
mov dx,buffer+2
mov ah,9
int 0x21
;exit
mov ah,0x4c
int 0x21
;buffer
buffer:
db 254; 255 ;len of buffer ;; my doc says the range is 1 to 254
; db 0 ;num of char read ;; you only need to reserve memory
; db 255 ;actual string ;; you only need to reserve memory
resb 255 ;; reserve memory (1 for length, 254 for text)
resb 1 ;; reserve memory for "$"
如果缓冲区是.COM程序中的最后一项,您实际上可以删除db 254
之后的所有行。