我在汇编中将两个数字作为字符串输入,然后将它们转换为整数,将它们相加,将结果转换为字符串,然后将其打印到终端。我遇到的主要问题是,我可以输入这两个数字,但输入后没有显示任何结果。
.global _start # Declare the global entry point
.intel_syntax noprefix # Use Intel assembly syntax without prefixes
.section .bss # Reserve uninitialized space for buffers
buffer1: .space 20
buffer2: .space 20
result_buffer: .space 20
.section .text
_start:
# Read the first number
mov rax, 0 # sys_read
mov rdi, 0 # File descriptor: stdin
lea rsi, [buffer1]
mov rdx, 20 # Max bytes to read
syscall
mov byte ptr [rsi + rax], 0 # null terminate the input buffer
lea rsi, [buffer1]
call string_to_int
mov r8, rax # Store the first integer in r8
# Read the second number
mov rax, 0 # sys_read
mov rdi, 0 # File descriptor: stdin
lea rsi, [buffer2]
mov rdx, 20 # Max bytes to read
syscall
mov byte ptr [rsi + rax], 0
lea rsi, [buffer2] # Use buffer2 for the second input
call string_to_int
mov r9, rax # Store the second integer in r9
add r8, r9 # r8 = r8 + r9
# Convert the result back to a string
mov rax, r8 # Move the result into rax
lea rsi, [result_buffer + 19] # Point to the end of result_buffer
call int_to_string
# Calculate the string length (RCX holds the number of digits)
mov rdx, rcx
# Write the result
mov rax, 1 # sys_write
mov rdi, 1 # File descriptor: stdout
lea rsi, [result_buffer] # Address of result_buffer
mov rdx, rcx # number of digits in result
syscall # Perform system call
# Exit
mov rax, 60 # sys_exit
xor rdi, rdi # Exit code: 0
syscall
# Subroutine: string_to_int
# Converts a null-terminated string to an integer
# Input: RSI points to the string
# Output: RAX contains the integer value
string_to_int:
xor rax, rax # Clear rax (result)
xor rcx, rcx # Clear rcx (multiplier, initially 0)
convert_loop:
movzx rdx, byte ptr [rsi] # Load byte from the string
cmp rdx, 10 # Check for newline (ASCII '\n')
je convert_done # Exit loop on newline
sub rdx, '0' # Convert ASCII to digit (0-9)
imul rax, rax, 10 # Multiply result by 10
add rax, rdx # Add the current digit to the result
inc rsi # Move to the next character
jmp convert_loop
convert_done:
ret
# Subroutine: int_to_string
# Converts an integer to a null-terminated string
# Input: RAX contains the integer, RSI points to the buffer
# Output: The buffer contains the string representation
int_to_string:
xor rcx, rcx # Digit counter (RCX will store the length)
xor rbx, rbx # Temporary register
convert_to_str:
xor rdx, rdx # Clear RDX for division
mov rbx, 10 # Divisor
div rbx # RAX = RAX / 10, RDX = remainder
add dl, '0' # Convert remainder to ASCII
dec rsi # Move backwards in buffer
mov [rsi], dl # Store ASCII character
inc rcx # Increment digit counter
test rax, rax # Check if RAX is 0
jnz convert_to_str # Continue if not zero
# Null-terminate the string
mov byte ptr [rsi + rcx], 0 # Null-terminate the string
ret
int_to_string
指向result_buffer
rsi
[正确地] 指向结果 ascii 字符串(比起开头来更接近末尾)。result_buffer
例如,如果我们输入
23
和36
,则加法的结果将是59
其
result_buffer
内容如下:因此,打印时我们应该只使用返回的值
rsi
,而不是执行以下操作:以下是更正后的代码: