Assembly 无法将整数打印到标准输出(程序集)
我试着做一个加法,然后把总数打印出来。但是什么都没印出来。我读取并将文本字符串更改为16位整数以存储在寄存器中Assembly 无法将整数打印到标准输出(程序集),assembly,int,stdout,Assembly,Int,Stdout,我试着做一个加法,然后把总数打印出来。但是什么都没印出来。我读取并将文本字符串更改为16位整数以存储在寄存器中 .section .data x: .int 40 y: .int 10 z: .int 30 v: .int 8 sum: .int 0 len: .int 2 # 2 bytes integer for length of sum .section .text .globl _start _start: movl x, %eax
.section .data
x: .int 40
y: .int 10
z: .int 30
v: .int 8
sum: .int 0
len: .int 2 # 2 bytes integer for length of sum
.section .text
.globl _start
_start:
movl x, %eax
addl y, %eax
addl z, %eax
subl v, %eax
movl %eax, sum
# put sum to standard out
movl len, %edx # third argument, length of sum
movl sum, %ecx # second argument: pointer to message
movl $1, %ebx # first argument: file handle (stdout)
movl $4, %eax # system call number (sys_write)
int $0x80 # call kernel
# exit
movl $0, %ebx
movl $1, %eax
int $0x80
好的!将整数转换为字符串时有什么方法吗 首先(仅出于测试目的),您可以使用C语言编写的帮助程序 您可以写入一个字节的消息类型和多个字节的数据。助手程序将转换它们。然后,命令行将如下所示:
assembler_program | helper_program
对于实际程序,必须将寄存器值转换为十进制。这是针对无符号数字的:
# Print out the value of eax...
# Prepare...
mov %esp, %ecx
sub $20, %esp
mov $10, %ebx
# Convert to decimal
next_digit:
xor %edx,%edx
div %ebx
add $'0', %dl
dec %ecx
mov %dl, 0(%ecx)
test %eax, %eax
jnz next_digit
# Calculate the size
lea 20(%esp), %edx
sub %ecx, %edx
# Call int $0x80
mov $4, %eax
mov $1, %ebx
int $0x80
# Clean up
add $20, %esp
对于签名号码,您需要以下附加代码:
test %eax,%eax
jns positive
neg %eax
push %eax
mov $4, %eax
mov $1, %ebx
mov $minus, %ecx
mov $1, %edx
int $0x80
pop %eax
positive:
# Now do the unsigned output from above
minus:
.byte '-'
您需要将整数转换为字符串进行打印。系统写入打印字节;它不能解释它们,好吧!将整数转换为字符串时有什么方法吗?