-2

Convert decial to binary in MASM32. Help me

include \masm32\include\masm32rt.inc

.code

start:
  call main
  exit

main proc
  LOCAL var1: DWORD
  LOCAL dem: DWORD
    
  mov var1, sval(input("Enter Binary Number: "))
  mov dem, strlen(str$(var1))
  mov ecx, dem
  mov ebx, 1
  mov dec, 0
  
lbl0:
  DEC ecx
  cmp ecx, 0
  jl lbl1
  
  mov eax, var1
  and eax, ebx
  mov edx, eax
  shl edx, cl
  add dec, edx
  shl ebx, 1
  jmp lbl0
  
lbl1:
  print chr$(13,10)
  print chr$("Decimal Equivalent: ")
  print str$(dec)
  print chr$(13,10)
  
  ret
  main endp
  
end start

here r my code but it don't work

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • "Here's my code, it doesn't work" is not a great way to ask for help. Tell us what you have tried, how you have troubleshot the problem, in what way the code does not perform as expected. – David Hoelzer Mar 16 '23 at 15:58

1 Answers1

1

The title askes for "Convert Binary to Decimal", but the text askes for "Convert decial to binary".
Your program attempts what's in the title...


mov var1, sval(input("Enter Binary Number: "))
...
print chr$("Decimal Equivalent: ")
print str$(dec)

Since you are using these advanced functions anyway, why not just solve the task with a mere str$(var1):

print chr$("Decimal Equivalent: ")
print str$(var1)

The 'conversion' loop that you show us isn't converting to decimal representation at all.

mov dem, strlen(str$(var1))
mov ecx, dem

strlen gives you the number of characters in a string, so then what is this particular loop count in ECX going to do?


See How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string) for how to convert your integer into a string of decimal characters.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76