Assembly 數字

數值數據普遍表示二進位系統。算術指令的操作上的二進位數據。當數字顯示在螢幕上,或從鍵盤輸入,它們是ASCII形式。

到目前為止,我們已經轉換成ASCII形式輸入數據進行算術運算的二進位結果轉換回二進位。下麵的代碼顯示:

section	.text
    global _start         ;must be declared for using gcc
_start:	;tell linker entry zaixian
	mov	eax,'3'
	sub     eax, '0'
	mov 	ebx, '4'
	sub     ebx, '0'
	add 	eax, ebx
	add	eax, '0'
	mov 	[sum], eax
	mov	ecx,msg
	mov	edx, len
	mov	ebx,1	;file descriptor (stdout)
	mov	eax,4	;system call number (sys_write)
	int	0x80	;call kernel
	mov	ecx,sum
	mov	edx, 1
	mov	ebx,1	;file descriptor (stdout)
	mov	eax,4	;system call number (sys_write)
	int	0x80	;call kernel
	mov	eax,1	;system call number (sys_exit)
	int	0x80	;call kernel
section .data
msg db "The sum is:", 0xA,0xD
len equ $ - msg
segment .bss
sum resb 1

上面的代碼編譯和執行時,它會產生以下結果:

The sum is:
7

然而,這樣的轉換是有一個系統開銷和組合語言的程式設計允許更有效的方式,處理數字的二進位形式。十進位數可以表示為兩種形式:

  • ASCII形式

  • BCD或二進位編碼的十進位形式

ASCII表示

在ASCII碼表示,十進位數字存儲ASCII字串。例如,十進位值1234被存儲為:

31	32	33	34H

其中,31H,32H是ASCII值1 ASCII值2,依此類推。有以下四個指令處理數字的ASCII表示:

  • AAA - ASCII Adjust After Addition

  • AAS - ASCII Adjust After Subtraction

  • AAM - ASCII Adjust After Multiplication

  • AAD - ASCII Adjust Before Division

這些指令不採取任何運算元,並承擔所需的運算元是在AL寄存器中。

下麵的示例使用AAS指令來說明這個概念:

section	.text
    global _start         ;must be declared for using gcc
_start:	;tell linker entry zaixian
	sub     ah, ah
	mov     al, '9'
	sub     al, '3'
	aas
	or      al, 30h
	mov     [res], ax

	mov	edx,len	;message length
	mov	ecx,msg	;message to write
	mov	ebx,1	;file descriptor (stdout)
	mov	eax,4	;system call number (sys_write)
	int	0x80	;call kernel

	mov	edx,1	;message length
	mov	ecx,res	;message to write
	mov	ebx,1	;file descriptor (stdout)
	mov	eax,4	;system call number (sys_write)
	int	0x80	;call kernel
	mov	eax,1	;system call number (sys_exit)
	int	0x80	;call kernel

section	.data
msg db 'The Result is:',0xa
len equ $ - msg
section .bss
res resb 1  

上面的代碼編譯和執行時,它會產生以下結果:

The Result is:
6

BCD 表示

BCD表示有兩種類型:

  • 未打包BCD表示BCD表示

  • 壓縮BCD表示

在壓縮BCD表示,每個位元組的存儲的二進位相當於十進位數字。例如,被存儲為數字1234:

01	02	03	04H

有兩種處理這些數字指令:

  • AAM - 乘法後ASCII調整

  • AAD - ASCII除法前調整

四個ASCII調整指令,AAA,AAS,AAM和AAD也可以用壓縮BCD表示。壓縮BCD碼表示,每個使用四位數字存儲。兩位十進位數被打包成一個位元組。例如,被存儲為數字1234:

12	34H

有兩種處理這些數字指令:

  • DAA - Decimal Adjust After Addition

  • DAS - decimal Adjust After Subtraction

乘法和除法包裝BCD表示不支持。

例子:

下麵的程式增加了兩個5位數的十進位數和顯示的總和。它使用上述的概念:

section	.text
    global _start         ;must be declared for using gcc

_start:	;tell linker entry zaixian

	mov     esi, 4  ;zaixianing to the rightmost digit
	mov     ecx, 5  ;num of digits
	clc
add_loop:
	mov 	al, [num1 + esi]
	adc 	al, [num2 + esi]
	aaa
	pushf
	or 	al, 30h
	popf
	mov	[sum + esi], al
	dec	esi
	loop	add_loop
	mov	edx,len	;message length
	mov	ecx,msg	;message to write
	mov	ebx,1	;file descriptor (stdout)
	mov	eax,4	;system call number (sys_write)
	int	0x80	;call kernel

	mov	edx,5	;message length
	mov	ecx,sum	;message to write
	mov	ebx,1	;file descriptor (stdout)
	mov	eax,4	;system call number (sys_write)
	int	0x80	;call kernel

	mov	eax,1	;system call number (sys_exit)
	int	0x80	;call kernel

section	.data
msg db 'The Sum is:',0xa
len equ $ - msg
num1 db '12345'
num2 db '23456'
sum db '     '

上面的代碼編譯和執行時,它會產生以下結果:

The Sum is:
35801

上一篇: Assembly 迴圈 下一篇: Assembly彙編 字串處理