【问题标题】:Manipulate string in assembly x86 (mov and print to screen)在汇编 x86 中操作字符串(移动并打印到屏幕)
【发布时间】:2014-10-22 01:14:21
【问题描述】:

我正在做一个更大的项目,但我被字符串操作困住了。我的汇编文件包括数学协处理器操作(它以“FINIT”启动协处理器),但我认为它根本不应该干扰。 基本上,我有一些每个 50 字节长的字符串:

$s db 50 dup (?), '$'
_cte_14 db "hello world", '$', 39 dup (?)

我需要将存储在变量“_cte_14”中的值分配给变量“$s” 我尝试使用寄存器来临时存储值,如下所示:

mov cx, _cte_14
mov $s, cx

但我收到“操作数类型不匹配”错误。

由于我知道 AX、BX、CX、DX 寄存器只保存 16 位,我想也许我需要处理第一个字符串字符的内存地址,所以我尝试了:

mov bx, offset _cte_14
mov $s, bx

但同样的错误出现了。

我正在使用 TASM 为 x86 处理器进行编译。实现这一目标的正确方法是什么?

非常感谢。

【问题讨论】:

  • 需要循环复制字符,或者使用字符串移动(movsb)指令。

标签: string assembly tasm


【解决方案1】:

循环复制字符的示例:

s db 51 dup ('$')
_cte_14 db "hello world"
len = ($ - _cte_14)    ; (current location - offset _cte_14)
40 dup ('$')

mov si, offset _cte_14 ; get source offset
mov di, offset s       ; get destination offset
mov cl, len            ; length of the string
P1:
mov al, [si]           ; get byte from DS:SI
mov [di], al           ; store byte to DS:DI
dec cl                 ; decrease counter, set zero flag if zero
jnz P1                 ; jump if zero flag is not set

-- 使用字符串指令和重复指令前缀的变化:

mov si, offset _cte_14
mov di, offset s
mov cx, len ; length of the string
cld         ; clear direction flag
rep movsb   ; copy from DS:SI to ES:DI, increase SI+DI, decrease CX, repeat CX times

【讨论】:

  • 您的回答中的一些 cmets 告诉您提供/修复的内容会很有用
  • 谢谢!!不过我还是有点困惑......因为你改变了我的 .data 声明,我不完全理解你的代码。我不确定“len”如何包含源字符串的长度。另外,不确定40 dup ('$') 做了什么。我在不更改 .DATA 段并手动插入源长度的情况下尝试了您的代码:mov cl, 12 ; length of "hello world$" 然后添加它以打印 $s 的内容:mov DX, OFFSET $s mov AH, 9 int 21h 但我得到的只是最初的“h”和一堆空白空格。
  • 没关系!刚刚发现缺少什么:您的循环代码很完美,只缺少inc siinc di。在dec cl 之前添加了它,它就像一个魅力。谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
相关资源
最近更新 更多