【发布时间】:2018-04-14 19:17:36
【问题描述】:
我编写了一个代码来将两个 8 位十六进制数相乘并应用移位和加法方法。我在名为“a”和“b”的变量中取了两个输入,它们是字节类型,并将它们存储在 al 和 bl 中。
在乘法过程中,al 左移,bl 右移。 稍后,在循环中,我将 ax(a) 添加到 dx 寄存器(初始化为 0)。 但是,问题是当 shl ax,1 被替换为 shl al,1 那么我没有得到所需的输出。即 - a=12 和 b=10 然后只打印 20 而不是 120。
请解释一下,为什么我不能写 shl al,1。
这里是代码
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro exit 0
mov rax,60
mov rdi,0
syscall
%endmacro
section .data
a db 12H
b db 10H
msg db 10,"Result : ",10
len equ $-msg
;------------------------
section .bss
tempbuff resb 16 ;temporary buffer for displaying the ascii
;------------------------
section .text
global _start
_start:
mov rdx,0
mov al,byte[a] ;al is multiplicand
mov bl,byte[b] ;bl is multiplier
mov rcx,8
lp:
shr bl,1
jnc haha
add dx,ax
haha:
shl ax,1 ;shl al,1 doesn;t work
loop lp
mov rbx,0
mov rbx,rdx
call hex_ascii ;converting the hex no into ascii
exit
hex_ascii:
mov rsi,tempbuff
mov rcx,16
mov rax,0
bah:
rol rbx,4
mov al,bl
and al,0FH
cmp al,09H
jbe add30
add al,07H
add30:
add al,30H
mov [rsi],al
inc rsi
loop bah
print tempbuff,16
ret
【问题讨论】:
-
如果您在
al中有一个 8 位值并且您想将其左移然后添加到dx,您需要将ax左移,否则您会丢失被乘数。换句话说,您希望继续将其左移,保留所有位,并在每次看到乘数中的 1 时将其添加到中间结果中。 -
但是当我将 al 位左移时,移位的位不会到达 ax 寄存器的 ah 部分。然后我将如何松开它们
-
shl al,1根本不影响ah。它只影响ah。这就是为什么你需要shl ax,1。