我需要帮助才能将十进制数分数转换为二进制数。这是我到目前为止,但每当我尝试运行它时它会给出分段错误。如果将其更改为32位,则完全正常。 我知道应该是64位的所有内容都是64位,但我不知道是否还需要进行任何其他更改才能使其正常工作。有人请帮帮我
extern printf
section .bss
dsave: resq 2
bsave: resq 1
section .data ;data section
fmt_float: db "%f",10,0 ; format of printf
fmt_char: db "%c",0 ; no '\n' thus no 10
fmt_dig: db "%1ld",0 ; print just one digit, e.g. 0 or 1
fmt_end: db 10, 0 ; just end line
dec1: db '1','2','4','.','3','7','5',0
bin1: dq 01010110110101B ;10101101.11101
section .text ;code section
global main ;standard gcc entry point
main:
;decimal to binary conversion
;integer to binary
push rbp ; set up stack
mov rbp,rsp
push rbx
push rdi
mov rax,0
mov al,[dec1] ; first digit
sub al,48 ; digit to binary
;imul rax,100 ; 1*100
mov rdi,1
l1:
mov rbx,0
mov bl,[dec1+rdi] ; 2
cmp bl,'.'
je f1
sub bl,48
imul rbx,10
add rax,rbx
inc rdi ; increment
jmp l1
f1:
mov [dsave],rax ; store 124
section .data
t: dq 10
global l2
section .text
mov rcx,rdi
add rcx,3
mov rax,0
mov al,[dec1+rcx]
sub al,48
shl rax,16
dec rcx
l2:
mov rdx,0
idiv qword[t]
mov rbx,0
mov bl,[dec1+rcx]
cmp bl,'.'
je f2
sub bl,48
shl rbx,16
add rax,rbx
loop l2
f2:
mov rdx, 0
idiv qword [t]
mov [dsave+16], rax
;mov rdi, dec1
;mov rsi, [dsave]
;mov al, 0
;and rax, 1
;shr rcx, 16 ; shift
;call printf ; call c function to print
;mov rdi,fmt_end ; print end of line
;mov rax, 0 ; no float
;call printf
;print
section .bss
abits: resb 17
section .data
fmt: db "%s",0
section .text
mov rax, [dsave]
mov rcx, 8
l3:
mov rax, 0
mov rdx, 0
shld rdx, rax, 1
add rdx, 48
mov [abits+rcx-1], dl
ror rax, 1
loop l3
mov byte [abits+7], '.'
mov byte [abits+8], 0
push qword abits
push qword fmt
call printf
add rsp, 8
mov rax, [dsave+16]
mov rcx, 16
l4:
mov rax, 0
mov rdx, 0
shld rdx, rax, 1
add rdx, 48
mov [abits+rcx-1], dl
ror rax, 1
loop l4
mov byte [abits+3],10
mov byte [abits+4],0
push qword abits
push qword fmt
call printf
add rsp,8
; convert binary to decimal
section .data
fmt_d db "%d.", 0
fmt_d2 db "%d", 10, 0
section .text
; convert binary integer to decimal part
mov rax, 0
mov rdx, 0
mov rdx, [bin1]
shld ax, dx, 11 ; shift binary value into rax
mov qword [bsave], rax
push qword [bsave]
push qword fmt_d
call printf
add rsp, 8
; convert binary fraction to decimal part
mov rbx, 0
mov rdx, [bin1]
shl rdx, 11
shld bx, dx, 5
mov rdx, 0
mov al, 1 ; quotient
shl bx, 3
mov ch, 0
loop1:
cmp ch, 4
je loop2
mov cl, 2
mov bh, 0
imul cl
shl bx, 1
cmp bh, 1
jne loop1
push rcx
push rax
mov al, 1
pop rcx
push rcx
push rdx
mov rdx, 0
break1:
idiv cl
pop rdx
add dx, ax
pop rax
pop rcx
inc ch
jmp loop1
loop2:
push qword rdx
push qword fmt_d2
call printf
add rsp,8
exit:
pop rdi ; restore
pop rbx
mov rsp,rbp
pop rbp
mov rax,0 ; no error
ret ; return