linux - IMUL指令寄存器

标签 linux assembly x86 x86-64 att

我正在编写这个简单的程序来计算递归序列的第 i 个元素。序列基本上看起来像

a(n)=a(n-1)*a(n-2)

前两个元素为 -1 和 -3。我使用 imul 进行乘法运算,并且由于我在网上的发现,我应该能够使用我想要的任何寄存器,但是 programm 为第三个元素返回 0。当切换到 add 时,它会按预期工作。 这是我递归调用函数并相乘的片段(如图所示,我使用堆栈来存储我的变量)

push %rcx
push %rax
call calculate
pop %rax
pop %rcx 
imul %rcx, %rbx

基本上问题是“为什么它不起作用”:P

附言。如果需要完整代码:

.data
STDOUT = 1
SYSWRITE = 1
HOW_MANY = 3                # which number to calculate
SYSEXIT = 60
EXIT_SUCCESS = 0
FIRST = -1                  # first element of the sequence
SECOND = -3                 # second element of the sequence
NUMBER_BEGIN = 0x30
OUTPUT_BASE = 10
NEW_LINE = '\n'
PLUS = '+'
MINUS = '-'

.bss
.comm textin, 512
.comm textout, 512
.comm text2, 512
.comm znak, 1

.text
.globl _start

_start:

#
# Calling function to calculate ith element
#
mov $HOW_MANY, %r8
sub $1, %r8
push %r8                    # push r8 (function argument) to stack
call calculate              # call function to calculate
add $8, %rsp                # removing parameter from stack

# now we should've have result in rbx  
#
mov $0, %r15 # Flaga znaku (domyślnie 0 = +)
cmp $0, %rbx
jge to_ascii # Pomiń jeśli liczba jest dodatnia
not %rbx # Odwrócenie bitów liczby i dodanie 1,
inc %rbx # aby uzyskać jej wartość bezwzględną.
mov $1, %r15 # Ustawienie flagi znaku na 1 = -.

to_ascii:
mov %rbx, %rax # result goes to rax
mov $OUTPUT_BASE, %rbx
mov $0, %rcx
jmp loop

loop:
mov $0, %rdx
div %rbx # divide rax by rbx, rest in rdx
add $NUMBER_BEGIN, %rdx # rest in rdx is a next position number
mov %dl, text2(, %rcx, 1)

inc %rcx
cmp $0, %rax
jne loop
jmp inverse

inverse:
mov $0, %rdi
mov %rcx, %rsi
dec %rsi
jmp inversev2

inversev2:
mov text2(, %rsi, 1), %rax
mov %rax, textout(, %rdi, 1)
inc %rdi
dec %rsi
cmp %rcx, %rdi
jle inversev2

push %rcx # legth of the answer goes to stack

mov $0, %r10 # want sign at the first position
movb $PLUS, znak(, %r10, 1)

cmp $0, %r15 # r15 register contains info about the sign
je next # 0 = +, so nothing has to be done

movb $MINUS, znak(, %r10, 1) # otherwise set it to minus

next:   # show sign
mov $SYSWRITE, %rax
mov $STDOUT, %rdi
mov $znak, %rsi
mov $1, %rdx
syscall

pop %rcx

movb $NEW_LINE, textout(, %rcx, 1)
inc %rcx

mov $SYSWRITE, %rax
mov $STDOUT, %rdi
mov $textout, %rsi
mov %rcx, %rdx
syscall

mov $SYSEXIT, %rax
mov $EXIT_SUCCESS, %rdi
syscall

# recursive function calculating ith element of a given sequence
# sequence =
# n_1 = -1
# n_2 = -3
# n_i = n_(i-1)*n_(i-2)
calculate:
push %rbp                   # push rbp to stack to save it's value
mov %rsp, %rbp              # now stack pointer is stored in rbp

sub $8, %rsp
mov 16(%rbp), %rax

cmp $1, %rax
jl first
je second

mov $0, %rcx

# wywołanie dla n_(i-1)
dec %rax

push %rcx
push %rax
call calculate
pop %rax
pop %rcx # przepisać na rejestry imula
imul %rcx, %rbx

# wywołanie dla n_(i-2)
dec %rax
push %rcx
push %rax
call calculate
pop %rax
pop %rcx
imul %rcx, %rbx

return:
mov %rcx, %rbx
mov %rbp, %rsp
pop %rbp
ret

first:
mov $FIRST, %rbx
mov %rbp, %rsp
pop %rbp
ret

second:
mov $SECOND, %rbx
mov %rbp, %rsp
pop %rbp
ret

最佳答案

您将 %rcx 设为零,然后乘以它,因此您的乘积始终为零。

也许你想改变

mov $0, %rcx

mov $1, %rcx

我认为你还需要扭转

imul %rcx, %rbx

imul %rbx, %rcx

(我不熟悉那种汇编程序的味道)

关于linux - IMUL指令寄存器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090362/

相关文章:

linux - 文件行在执行时被更改

linux - 如何过滤某个字段中有尾随空格的行?

c++ - 将参数从 C++ 传递到 cout 中的程序集

assembly - 有序/无序比较是什么意思?

assembly - 通过 USB 驱动器启动的自定义引导加载程序在某些计算机上产生不正确的输出

linux - 无法为 docker 容器执行脚本?

linux - 使用ffmpeg时如何在shell脚本中创建具有输入结构的动态文件夹?

c - 64位机上栈帧的创建

c - 汇编器中的简单增量

c - 需要在IDA Pro中解读C代码