x86汇编(NASM)中字符串与变量拼接及赋值问题咨询
Got it, let's break down how to do this in NASM x86 assembly. Unlike high-level languages, you don't get built-in string concatenation with variables—you'll need to handle two key tasks yourself: converting integers to ASCII strings, and manually copying each piece of your final string into a target buffer. Here's a complete, working example that matches your pseudocode:
section .data ; String fragments we'll concatenate str_prefix db "values are: " prefix_len equ $ - str_prefix ; Calculate length of prefix str_middle db " and " middle_len equ $ - str_middle str_newline db 0xA ; Newline character newline_len equ $ - str_newline ; Numeric variables a dd 1 b dd 2 section .bss ; Temporary buffer for converted integer strings (max 10 digits + null terminator) num_buf resb 12 ; Buffer to hold the final concatenated result (plenty of space for our example) result_buf resb 128 section .text global _start ; Helper function: Convert 32-bit integer to ASCII string ; Input: eax = integer to convert, edi = address of buffer to store result ; Output: eax = length of the converted string int_to_str: push ebx push ecx push edx push esi mov esi, edi ; Save start of buffer for length calculation later mov ecx, 0 ; Counter for number of digits ; Handle negative numbers (optional, included for completeness) test eax, eax jns .positive neg eax mov byte [edi], '-' inc edi inc ecx .positive: ; Extract digits by dividing by 10 .div_loop: mov edx, 0 mov ebx, 10 div ebx ; eax = eax/10, edx = remainder (current digit) add dl, '0' ; Convert digit to ASCII character push edx ; Push to stack (digits are reversed here) inc ecx test eax, eax jnz .div_loop ; Pop digits from stack into buffer (to get correct order) .pop_loop: pop edx mov byte [edi], dl inc edi dec ecx jnz .pop_loop ; Add null terminator (useful for debugging, optional for pure concatenation) mov byte [edi], 0 ; Calculate and return string length sub edi, esi mov eax, edi pop esi pop edx pop ecx pop ebx ret _start: ; Step 1: Convert variable 'a' to string and save its length mov eax, [a] mov edi, num_buf call int_to_str mov ebx, eax ; ebx = length of a's string ; Step 2: Copy prefix to result buffer mov esi, str_prefix mov edi, result_buf mov ecx, prefix_len rep movsb ; Copy 'prefix_len' bytes from esi to edi ; Step 3: Copy converted 'a' string to result buffer mov esi, num_buf mov ecx, ebx rep movsb ; Step 4: Copy middle string (" and ") to result buffer mov esi, str_middle mov ecx, middle_len rep movsb ; Step 5: Convert variable 'b' to string and save its length mov eax, [b] mov edi, num_buf call int_to_str mov ebx, eax ; ebx = length of b's string ; Step 6: Copy converted 'b' string to result buffer mov esi, num_buf mov ecx, ebx rep movsb ; Step 7: Add newline to result mov esi, str_newline mov ecx, newline_len rep movsb ; Calculate total length of final result mov edx, edi sub edx, result_buf ; Print the final string using Linux syscall mov eax, 4 ; sys_write system call number (x86 Linux) mov ebx, 1 ; File descriptor: stdout mov ecx, result_buf int 0x80 ; Exit program cleanly mov eax, 1 ; sys_exit system call number mov ebx, 0 int 0x80
1. Integer to ASCII Conversion
The int_to_str function handles turning binary integers into human-readable ASCII strings. It works by:
- Dividing the integer by 10 repeatedly to extract individual digits
- Converting each digit to its ASCII equivalent (adding
'0'to the numeric value) - Storing digits in reverse order on the stack, then popping them back to the buffer to get the correct sequence
2. String Concatenation
We use the rep movsb instruction to copy each string fragment into the result_buf:
esipoints to the source stringedipoints to the current position in the target bufferecxholds the number of bytes to copy- After each copy,
ediautomatically advances to the next empty position in the buffer
3. Compile & Run
To test this code on a Linux x86 system:
nasm -f elf32 your_program.asm -o your_program.o ld -m elf_i386 your_program.o -o your_program ./your_program
You'll see the output: values are: 1 and 2 (followed by a newline)
内容的提问来源于stack exchange,提问作者lunatic955

