コメントに記載されているように、ヌルターミネータ(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
EDIT:
以下OFFSET_INTO_DATA
とNUM_CHARS
に基づいて選択サブストリングをプリントアウト32ビットLinuxアプリケーションとしてrunableテスト実装であります(注:アルゴリズムは同じですが、レジスタは変更されています)
でコンパイル
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
私も最近のページに、メインページ上の私のエントリが表示されないことから、スペースを追加することにより、編集とコメントしました。 – LavaMaster107
それは表示されていません... – LavaMaster107
これをブラウズして見つけました。あなたには幸運があると思います。 –