News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Decimal to Binary in Masm32

Started by kondwani1, November 13, 2018, 07:14:17 PM

Previous topic - Next topic

kondwani1

I really need help with a program that can convert decimal to decimal the following program isn't running in MASM32
;=============================================================================================
.LC0:
        .string "\n\n Function : convert decimal to binary :"
.LC1:
        .string "-------------------------------------------"
.LC2:
        .string " Input any decimal number : "
.LC3:
        .string "%d"
.LC4:
        .string "\n The Binary value is : %ld\n\n"
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     edi, OFFSET FLAT:.LC0
        call    puts
        mov     edi, OFFSET FLAT:.LC1
        call    puts
        mov     edi, OFFSET FLAT:.LC2
        mov     eax, 0
        call    printf
        lea     rax, [rbp-12]
        mov     rsi, rax
        mov     edi, OFFSET FLAT:.LC3
        mov     eax, 0
        call    __isoc99_scanf
        mov     eax, DWORD PTR [rbp-12]
        mov     edi, eax
        call    toBin
        mov     QWORD PTR [rbp-8], rax
        mov     rax, QWORD PTR [rbp-8]
        mov     rsi, rax
        mov     edi, OFFSET FLAT:.LC4
        mov     eax, 0
        call    printf
        mov     eax, 0
        leave
        ret
toBin:
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-36], edi
        mov     QWORD PTR [rbp-8], 0
        mov     QWORD PTR [rbp-16], 1
        jmp     .L4
.L5:
        mov     eax, DWORD PTR [rbp-36]
        cdq
        shr     edx, 31
        add     eax, edx
        and     eax, 1
        sub     eax, edx
        cdqe
        mov     QWORD PTR [rbp-24], rax
        mov     rax, QWORD PTR [rbp-24]
        imul    rax, QWORD PTR [rbp-16]
        add     QWORD PTR [rbp-8], rax
        mov     rdx, QWORD PTR [rbp-16]
        mov     rax, rdx
        sal     rax, 2
        add     rax, rdx
        add     rax, rax
        mov     QWORD PTR [rbp-16], rax
        mov     eax, DWORD PTR [rbp-36]
        mov     edx, eax
        shr     edx, 31
        add     eax, edx
        sar     eax
        mov     DWORD PTR [rbp-36], eax
.L4:
        cmp     DWORD PTR [rbp-36], 0
        jne     .L5
        mov     rax, QWORD PTR [rbp-8]
        pop     rbp
        ret

hutch--

There are a number of problems here, the registers are 64 bit, there is no build information (which assembler and linker), no include files even though it looks like you are trying to call MSVCRT functions or VS runtime functions.

Try telling us more about what you are trying to do and with what tools you are trying to do it with and someone may be able to help you.

kondwani1

Thank You... what i am try to do is get user input as a decimal then convert it to binary. I really need help

hutch--

The problem is we are not magicians, we don't know what assembler you are using, what include files you are using or what libraries you are using. The code you have posted is not complete and there is no way to work it out. Until you can tell us enough to comprehend what you are doing, there is nothing we can do to help you.

kondwani1

I have now done this but its still giving me missing operator expression
;=================================================================
.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include  \masm32\include\masm32rt.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

; Objective: To print the binary equivalent of
; ASCII character code.
; Input: Requests a character from keyboard.
; Output: Prints the ASCII code of the
; input character in binary.

.stack 100h
.data
char_prompt db "Please input a number: ",0
out_msg1 db "Number  ",0
out_msg2 db " in binary is ",0


.data?
   
    buffer db   100 dup(?)

.CODE
start:




invoke StdOut, addr char_prompt ; request a char
push 100
push offset buffer ; read input character
call StdIn

mov eax, offset buffer
int 21 h

xor edx, edx
mov eax, offset buffer
int 21 h
mov ecx, 2
div ecx
jz print_0


 

invoke StdOut, addr out_msg1
invoke StdOut, addr buffer




invoke StdOut, addr out_msg2

mov AH,80H ; mask byte = 80H

mov ECX,8 ; loop count to print 8 bits

print_bit:

test buffer,AH ; test does not modify AL

jz print_0 ; if tested bit is 0, print it

print "1"  ; otherwise, print 1

jmp skip1

print_0:

print "0" ; print 0
skip1:
call dwtoa

shr buffer,1 ; right-shift mask bit to test

; next bit of the ASCII code

loop print_bit


exit:
  push 0
  call ExitProcess
end start

Mikl__

format PE GUI
include 'win32ax.inc'
; import, code and data in the same section
   mov eax,12345
   sub esp,80; buffer for wsprintf and MessageBox
   mov esi,esp
   push eax eax
   or eax,eax
   jz a0
   bsr ecx,eax
   inc ecx
   mov edx,fmt+35
   sub edx,ecx
@@:shr eax,1
   adc byte [edx+ecx],0
   loop @r
a0:  invoke wsprintf,esi,fmt
   invoke MessageBox,NULL,esi,wTitle,MB_OK
   add esp,4*4+80;params for wsprintf and buffer
   invoke ExitProcess,0
wTitle  db   'rococo',0
fmt db "eax=00000000000000000000000000000000b",10,"eax=%d",10, "eax=%08Xh",0
data import
library user32,'USER32.DLL',\
         kernel32,'kernel32.dll'
import user32,\
        wsprintf,  'wsprintfA',\
        MessageBox,'MessageBoxA'
import kernel32,\
        ExitProcess,'ExitProcess'
end data

hutch--

First,

Put this at the top of the source.

include  \masm32\include\masm32rt.inc

Remove the rest.

Mikl__

Hi, kondwani1!
It was FASM, in MASM32 is include  \masm32\include\masm32rt.inc

.data   
wTitle  db   'kondwani1',0
fmt db "eax=00000000000000000000000000000000b",10,"eax=%d",10, "eax=%08Xh",0
.code
start:mov eax,12345
   sub esp,80; buffer for wsprintf and MessageBox
   mov esi,esp
   push eax
   push eax
   or eax,eax
   jz a0
   bsr ecx,eax
   inc ecx
   mov edx,offset fmt+35
   sub edx,ecx
@@:shr eax,1
   adc byte ptr [edx+ecx],0
   loop @b
a0:  invoke wsprintf,esi,offset fmt
   invoke MessageBox,NULL,esi,offset wTitle,MB_OK
   add esp,4*4+80;params for wsprintf and buffer
   invoke ExitProcess,NULL
end start

kondwani1

 :t :lol: 
Thank you very much its workin!!!


So How can you now allow user to input a value?

Mikl__

not at all!
Processing the command line or read about the Dialogs.
You have the same bad English as me. What country are you from?

kondwani1

HAHAHAHAHAHA Am from Zambia and Yourself?

Mikl__

I'm from Russia. There is winter in our contry now

kondwani1

Well Nice to meet you :biggrin:

I am asking for help, where a user can enter an interger
but with this code the program isnt reading a number en puttin it in eax register
;=============================================

include  \masm32\include\masm32rt.inc

.data   
wTitle  db   'DECIMAL TO BINARY',0
fmt db "eax=00000000000000000000000000000000 In Binary",10,"eax=%d",10, "eax=%08X In Hexa",0

prompt db "Enter a Number: ",'$'



.code
start:
    invoke StdOut, ADDR prompt; request a char
    push 100
    push offset prompt ; read input character
    call StdIn
   
 

   mov eax,offset prompt
   sub esp,80; buffer for wsprintf and MessageBox
   mov esi,esp
   push eax
   push eax
   or eax,eax
   jz a0
   bsr ecx,eax
   inc ecx
   mov edx,offset fmt+35
   sub edx,ecx
@@:shr eax,1
   adc byte ptr [edx+ecx],0
   loop @b
a0:  invoke wsprintf,esi,offset fmt
   invoke MessageBox,NULL,esi,offset wTitle,MB_OK
   add esp,4*4+80;params for wsprintf and buffer
   invoke ExitProcess,NULL
end start


TimoVJL

@Mikl__
Very nice scenery.
Is the bear sleeping now?
May the source be with you

Mikl__

Hi, Timo!
Wild bears are already asleeping in forest, and the pet cub sleeps only at night. He lives in warm and eats every day, he doesn't sleep for the whole winter and he not needed saves fat reserves until spring