The MASM Forum

General => The Campus => Topic started by: kondwani1 on November 13, 2018, 07:14:17 PM

Title: Decimal to Binary in Masm32
Post by: kondwani1 on November 13, 2018, 07:14:17 PM
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
Title: Re: Decimal to Binary in Masm32
Post by: hutch-- on November 13, 2018, 09:56:16 PM
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.
Title: Re: Decimal to Binary in Masm32
Post by: kondwani1 on November 14, 2018, 08:03:22 PM
Thank You... what i am try to do is get user input as a decimal then convert it to binary. I really need help
Title: Re: Decimal to Binary in Masm32
Post by: hutch-- on November 14, 2018, 08:11:13 PM
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.
Title: Re: Decimal to Binary in Masm32
Post by: kondwani1 on November 14, 2018, 09:40:05 PM
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
Title: Re: Decimal to Binary in Masm32
Post by: Mikl__ on November 14, 2018, 09:53:09 PM
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
(http://wasm.in/forum/attachments/01-png.160/?temp_hash=dc6500b44c2e49ad34a79492d6439522)
Title: Re: Decimal to Binary in Masm32
Post by: hutch-- on November 14, 2018, 09:54:33 PM
First,

Put this at the top of the source.

include  \masm32\include\masm32rt.inc

Remove the rest.
Title: Re: Decimal to Binary in Masm32
Post by: Mikl__ on November 14, 2018, 11:18:08 PM
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
Title: Re: Decimal to Binary in Masm32
Post by: kondwani1 on November 14, 2018, 11:31:12 PM
 :t :lol: 
Thank you very much its workin!!!


So How can you now allow user to input a value?
Title: Re: Decimal to Binary in Masm32
Post by: Mikl__ on November 14, 2018, 11:35:37 PM
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?
Title: Re: Decimal to Binary in Masm32
Post by: kondwani1 on November 14, 2018, 11:58:30 PM
HAHAHAHAHAHA Am from Zambia and Yourself?
Title: Re: Decimal to Binary in Masm32
Post by: Mikl__ on November 15, 2018, 12:03:06 AM
I'm from Russia. There is winter in our contry now
(https://sun-news.ru/uploads/posts/2017-10/1507977962_364849-svetik.jpg)
Title: Re: Decimal to Binary in Masm32
Post by: kondwani1 on November 15, 2018, 12:12:30 AM
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

Title: Re: Decimal to Binary in Masm32
Post by: TimoVJL on November 15, 2018, 12:35:20 AM
@Mikl__
Very nice scenery.
Is the bear sleeping now?
Title: Re: Decimal to Binary in Masm32
Post by: Mikl__ on November 15, 2018, 12:47:43 AM
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
Title: Re: Decimal to Binary in Masm32
Post by: Mikl__ on November 15, 2018, 12:58:56 AM
kondwani1
look masm32\examples\resdlg
Title: Re: Decimal to Binary in Masm32
Post by: aw27 on November 15, 2018, 01:58:07 AM
The easiest, quick and dirty way to input an integer is probably scanf but it has its quirks, namely it is not interactive - i.e it reads the whole line before looking for what you inputted.

Official language in Zambia is English, lol.
Title: Re: Decimal to Binary in Masm32
Post by: kondwani1 on November 20, 2018, 09:06:36 AM
Can you help me please?
Title: Re: Decimal to Binary in Masm32
Post by: jj2007 on November 20, 2018, 01:07:08 PM
I have commented your code, and changed a few lines. Study it carefully, especially the adc instruction. Btw it would really help if you could explain in a few words what you are doing here. This looks like homework, right? What are you studying?

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, e.g. 12345
  call StdIn ; you get the text 12345 at offset prompt

  invoke atodw, offset prompt ; ascii to dword conversion
  ; NO mov eax, offset prompt ; NO, this would destroy your result from atodw
  sub esp, 80 ; buffer for wsprintf and MessageBox
  mov esi, esp
  push eax ; push the binary 12345 twice for
  push eax ; the %d and %08X format string entries
  or eax, eax
  jz a0
  bsr ecx, eax ; loads the destination with an index to first set bit
  inc ecx
  mov edx, offset fmt+35
  sub edx, ecx ; now start the loop for the binary output

@@:  shr eax, 1 ; shift right one bit, set or clear the carry flag
   adc byte ptr [edx+ecx], 0 ; [edx+ecx] has "0" - see fmt db above; if the carry is set, 0 becomes "1"
   loop @b

a0:
  push offset fmt
  push esi
  call wsprintf ; invoke wsprintf,esi,offset fmt, ecx, ecx
  invoke MessageBox,NULL,esi,offset wTitle,MB_OK
  add esp,4*4+80;params for wsprintf and buffer
  invoke ExitProcess,NULL
end start
Title: Re: Decimal to Binary in Masm32
Post by: kondwani1 on November 22, 2018, 12:04:55 AM
Studying Computer Science... Thank you very Much