什么意思名称:.space 2 ^ 32 这意味着我保留的空间等于2的功率32?
当我将数字除以16时如何得到余数? 我需要将数字从Dec改为Hex,我想一想:
答案 0 :(得分:0)
除以2的幂的除法就像用除数减去1来计算你的数字一样简单,所以你的除数是16,你需要和15一起运算。AND运算的结果就是你的余数。
同样,如果向右移4位(2 ^ 4 = 16),你将得到同一分区的商。
要转换为十六进制,只需将上面的内容放在一个循环中,您就可以获得任何字节大小的数字的余数,一次一个半字节,然后您需要转换为ASCII十六进制表示(从右到左)。
(同样,此方法仅适用于在特定情况下为2的幂的除数。)
要将半字节从二进制(0到15)更改为ASCII十六进制数字,您还可以使用以下快捷方式,但前提是您的处理器支持DAA(十进制加法调整)操作:
add #$90
daa
adc #$40
daa
(具体说明当然取决于您的特定汇编语言。)
答案 1 :(得分:0)
.stack 100h
.data
hex_char db ?,'$' ;HEX CHARACTERS TO DISPLAY.
.code
;INITIALIZE DATA SEGMENT.
mov ax,@data
mov ds,ax
mov ax, 30922 ;ANY NUMBER TO CONVERT.
call number2hex
;FINISH PROGRAM.
mov ax, 4c00h
int 21h
;-------------------------------------
;THIS PROC CONVERTS AX INTO HEX AND DISPLAYS RESULT.
;ALGORITHM : DIVIDE AX BY 16, CONVERT REMAINDER INTO
;HEX (0..9,A..F), REPEAT UNTIL QUOTIENT == 0. DIGITS
;EXTRACTED ARE BEEN PRESERVED IN STACK BECAUSE THEY
;HAVE TO BE DISPLAYED IN REVERSE ORDER.
;REMAINDER IS ALWAYS LESS THAN 16.
proc number2hex
;PHASE 1 : EXTRACT ALL DIGITS INSERTING THEM IN STACK.
mov bx, 16 ;DIVIDE NUMBER BY 16.
mov cx, 0 ;COUNTER FOR DIGITS. NECESSARY LATER.
extracting:
mov dx, 0 ;CLEAR DX BECAUSE DIV DIVIDES DX:AX / BX.
div bx ;DX:AX / BX. RESULT: AX=QUOTIENT, DX=REMAINDER.
cmp ax, 0 ;IF ( QUOTIENT==0 ) PROCESS IS OVER.
je last_digit
push dx ;PRESERVE DIGIT FOR LATER CONVERSION.
inc cx ;COUNT THIS DIGIT.
jmp extracting ;REPEAT PROCESS.
;WHEN PROCESS IS OVER REMAINDER STILL HOLDS LAST DIGIT.
last_digit:
push dx ;PRESERVE DIGIT FOR LATER CONVERSION.
inc cx ;COUNT THIS DIGIT.
;PHASE 2 : EXTRACT DIGITS FROM STACK IN REVERSE ORDER
;CONVERTING THEM TO HEX AND DISPLAY THEM.
converting:
pop dx ;GET DIGIT FROM STACK.
call convert_hex ;CONVERT DL INTO HEX.
call display_hex ;DISPLAY DL.
loop converting ;CX-1. IF ( CX > 0 ) GOTO CONVERTING.
ret
endp
;-------------------------------------
;GETS THE VALUE IN "DL" AND REPLACES IT
;BY THE CORRESPONDING HEX VALUE.
proc convert_hex
cmp dl, 10
jb its_a_number ;IF ( DL < 10 ) IT'S A NUMBER.
;IF NO JUMP IT'S A LETTER.
add dl, 55 ;CONVERT 10..15 TO 'A'..'F'.
jmp end_convert ;SKIP "ITS_A_NUMBER".
its_a_number:
add dl, 48 ;CONVERT 0..9 TO '0'..'9'.
end_convert:
ret
endp
;-------------------------------------
;GET HEX DIGIT IN "DL", TRANSFER VALUE
;TO "HEX_CHAR", DISPLAY "HEX_CHAR".
proc display_hex
mov hex_char, dl
mov dx, offset hex_char
mov ah, 9
int 21h
ret
endp
注意有时使用DX,有时使用DL。这是因为堆栈需要两个字节(DX),但余数总是8位长(DL),因为除以16会抛出小于16(0..15)的余数。