News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Decimal to Binary in Masm32

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

Previous topic - Next topic

Mikl__

kondwani1
look masm32\examples\resdlg

aw27

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.

kondwani1


jj2007

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

kondwani1

Studying Computer Science... Thank you very Much