我目前正在从事一个项目,为了存储起见,我想在汇编中截取一个变量,并(可选)使该寄存器的值(例如eax)成为可能。
我将需要使用英特尔语法与NASM一起使用的代码。
例如,如果变量“ msg”设置为“ 29ak49”,我希望将其中的一部分(例如“ 9ak4”,并放入寄存器或类似的东西中。
正如注释中提到的Peter Cordes,您总是可以在现有字符串的缓冲区中添加一个空终止符(0
)来进行右截断;如果您不介意修改原始字符串数据。
下面的示例将检索子字符串而不修改原始字符串。
如果您有变量的地址,并且知道要在何处截断,则可以获取数据起始位置的地址,并向左截断添加偏移量。要截断,您可以从新的偏移量中读取所需数量的字符。
例如x86
:
msg db '29ak49' ; create a string (1 byte per char)
;; left truncate
mov esi, msg ; get the address of the start of the string
add esi, OFFSET_INTO_DATA ; offset into the string (1 byte per char)
;; right truncate
mov edi, NUM_CHARS ; number of characters to take
.loop:
movzx eax, byte [esi] ; get the value of the next character
;; do something with the character in eax
inc esi
dec edi
jnz .loop
;; end loop
编辑:
以下是作为32位Linux应用程序的可运行测试实现,该应用程序打印出基于OFFSET_INTO_DATA
和NUM_CHARS
选择的子字符串(note:算法相同,但是寄存器已更改) :
section .text
global _start
_start:
;; left truncate
mov esi, msg ; get the address of the start of the string
add esi, OFFSET_INTO_DATA ; offset into the string (1 byte per char)
;; right truncate
mov edi, NUM_CHARS ; number of characters to take
.loop:
mov ecx, esi ; get the address of the next character
call print_char_32
inc esi
dec edi
jnz .loop
jmp halt
;;; input: ecx -> character to display
print_char_32:
mov edx, 1 ; PRINT
mov ebx, 1 ;
mov eax, 4 ;
int 0x80 ;
ret
halt:
mov eax, 1 ; EXIT
int 0x80 ;
jmp halt
section .data
msg db '29ak49' ; create a string (1 byte per char)
OFFSET_INTO_DATA EQU 1
NUM_CHARS EQU 3
编译:
nasm -f elf substring.asm ; ld -m elf_i386 -s -o substring substring.o