以下汇编代码有什么问题? [英] What's wrong with following assembly code?

查看:93
本文介绍了以下汇编代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取输入字符串的代码

在Windows计算机上使用nasm进行组装:nasm file.asm -o file.com

assemble using nasm on windows machine: 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个字符的字符串的工作文件,之后它会发出垃圾.

It is working file for string up to 2 char in length and after that it is giving out garbage.

推荐答案

在这里应解决此问题.参见带有;;的行.

Here's how it should be fixed. See the lines with ;;.

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之后的所有行.

If the buffer is the last thing in your .COM program, you can actually remove all lines after db 254.

这篇关于以下汇编代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆