nasm - Assembly Language -


i need following covert problem in assembly 64-bit nasm. convert decimal binary , vice versa. when print dont right answers integer part of binary decimal , fraction part of binary decimal. comments appreciated. feedback.

here output:

the integer part of decimal binary is: 1111100  fraction part of decimal binary  .011  integer part of binary decimal   173 fraction part of binary decimal   .768 

here code:

 ;convert:       convert numbers between bases                                                                                                                                                                ;compile:       nasm -f elf64 -l convert.lst convert.asm                                                                                                                                                     ;link:          gcc -m64 -o convert convert.o                                                                                                                                                                ;run:           ./convert                                                                                                                                                                                      extern printf                  section .data data1:    db      '1','2','4','.','3','7','5' data2:    dd      01010110110101b m:            db      2 i:            dq      1000.0                                   ; print format                                                                                                                                                               fam:        db      "conversion between bases",10,0 fam2:      db      "the integer part of decimal binary is: %ld%ld%ld%ld%ld%ld",0 fam3:      db      "the fraction part of decimal binary  .%ld%ld%ld",10,0 fam4:      db      "the integer part of binary decimal   %ld",10, 0 fam5:      db      "the fraction part of binary decimal   .%ld",10,0 fam6:      db      "%ld",10,0          section .bss                    ;uninitialized spaces                                                                                                                                                 b:            resq    1 c:            resq    10 d:            resq    1                   section .text           ;instruction code segment                                                                                                                                                            global main             ;for gcc standard linking                                                                                                                                             main:          ;; converting integer part of data1                                                                                                                                                                              push rbp                ;setup stack                                                                                                                                                                         mov rdi, fam                 mov rax, 0                 mov al, [data1]         ;move first index of data1                                                                                                                                                           sub rax, 48             ;convert ascii code decimal                                                                                                                                                       imul rax,100            ;multiply place value                                                                                                                                                         mov [b], rax                 mov rax, 0                 mov al,[data1+1]        ;move second index register                                                                                                                                                   sub rax, 48                 imul rax, 10                 add rax, [b]                 mov [b], rax                 mov rax, 0                 mov al, [data1+2]       ;move third index register                                                                                                                                                sub rax, 48                 add rax, [b]            ;the decimal value of number                                                                                                                                                     mov [b], rax                   mov rdx, 0                 mov rcx, 2                 div rcx                 mov [b], rax                 mov rsi, rdx                 mov rax, 0                  call printf            ;print first line of output                                                                                                                                                   mov r8, 0*8             ;count                                                                                                                                                                loop1:                  mov rax, 0                 mov rax, [b]            ;load b register                                                                                                                                                                  mov rdx, 0                 mov rcx, 2              ;load 2 register rcx                                                                                                                                                          div rcx                 ;divide 2                                                                                                                                                                         mov qword[c+r8],rdx     ;save reminder in array c                                                                                                                                                    mov [b], rax                 add r8,8                ;increament count                                                                                                                                                                cmp r8, 5*8             ;compare if less or equal                                                                                                                                                 jle loop1               ;jump instruction                                                                                                                                                                      mov rdi, fam2           ;format print out converted binary number                                                                                                                                     mov rsi, qword[c+5*8]                 mov rdx, qword[c+4*8]                 mov rcx, qword[c+3*8]         mov r8, qword[c+2*8]                 mov r9, qword[c+1*8]                 mov r10, qword[c+0*8]                 mov rax, 0                 call printf                  mov rdi, fam6           ;format printout last remainder                                                                                                                                               mov rsi, 0                 mov rax, 0                  call printf          ;; converting fraction part of decimal number                                                                                                                                                                    mov rax, 0                 mov al, [data1+4]       ;load fourth index                                                                                                                                                               sub rax, 48             ;change decimal number                                                                                                                                                            imul rax, 100           ;multiply place value                                                                                                                                                    mov [b], rax                  mov rax, 0                 mov al,[data1+5]         ;load fifth index of integer                                                                                                                                                sub rax, 48              ;convert decimal number                                                                                                                                                      imul rax, 10                 add rax, [b]                 mov [b], rax                 mov rax, 0                 mov al, [data1+6]       ;move the 6th index of data 1                                                                                                                                                  sub rax, 48             ;convert decimal                                                                                                                                                                   add rax, [b]                 mov [b], rax                 mov rax, 0                mov rcx, 0   loop2:                 mov rax, [b]    ;instead of working on .375 work on 375                                                                                                                                                      imul rax, 2                 cmp rax, 1000   ;compare if multiplication 100                                                                                                                                                   mov rdx, 0                 mov [c+8], rdx  ; if multiplication less 1000 store                                                                                                                                              add rcx, 1      ; 0 in c array                                                                                                                                                                           jle loop2       ;if rax less 1000 jump loop2                                                                                                                                                         jg loop3        ;if rax greater 1000 jump loop3                                                                                                                                    loop3:          mov rdx, 1      ; if rax grater rax store 1 in array [c]                                                                                                                                         mov [c+1*8], rdx                 add rcx, 1                 sub rax, 1000   ;after storing 1 in array c subtruct 1000 start th                                                                                                                                        imul rax, 2     ; loop again                                                                                                                                                                             cmp rax, 1000                 je loop4  loop4:          mov rdx, 1                 mov [c+3*8], rdx                 mov rax, 0                 mov rdi, fam3                 mov rax, [c]                 mov rsi, rax                 mov rdx, [c+8]                 mov rcx, [c+2*8]                 call printf                   mov rax, 0                  mov rdi, fam4                 mov rdx, [data2]                 shld ax, dx, 11                 mov rsi, rax                 mov rax, 0                 call printf                    mov rax, 0                 mov rdx, 0                  mov rdx, [data2]                 shl rdx, 11                 shld bx, dx, 5                  mov rdx, 0                 mov al, 1                 shl bx, 3                 mov ch, 0  loop5:                 cmp ch,4                 je loop6                 mov cl, 2                 mov bh, 0       imul cl                 shl bx, 1                 inc ch                 cmp bh, 1                 jne loop5                   push rcx                 push rax                 mov al, 1                 pop rcx                 push rcx                 push rdx                 mov rdx, 0  loop7:                 idiv cl                 pop rdx                 add dx, ax                 pop rax                 pop rcx                 inc ch                 jmp loop5  loop6:                 mov rdi, fam5                 mov rsi, rdx                 mov rax, 0                 call printf           pop rbp                 mov rax, 0                 ret 


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -