码取输入字符串
组装使用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 char和它给了垃圾之后。
下面是它如何被固定。 见线与;;
。
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
。