hi!
can any one help & give idea to write a program in MASM to convert BCD number to binary?
13 replies to this topic
#1
Posted 14 May 2010 - 11:39 PM
|
|
|
#2
Posted 23 May 2010 - 07:00 AM
So for example if you had the number 256, you'd want to convert from
0010 0101 0110
to
0000000100000000?
It shouldn't be too awful. I'll give it a go.
This obviously has some limitations, and I haven't tested it (I just wrote the code here on the forum) but everything looks fairly decent. Give it a try and let me know if it works?
0010 0101 0110
to
0000000100000000?
It shouldn't be too awful. I'll give it a go.
bcdToInt proc NEAR32 stdcall uses eax ebx ecx edx esi lpValue:DWORD, iLength:DWORD mov ebx,lpValue ;ebx = address pointing to start of bcd values mov ecx,iLength ;ecx = number of bytes of bcd string. ;could be done away with if you're using a fixed length xor esi,esi ;zero out a counter. .WHILE(ecx > 0) mov al, [ebx] ;move the first byte of the bcd value into al. and ax,000Fh ;zero the top 4 bits of the al register. ;two bytes are needed for the stack though push ax ;push the digit onto the stack inc esi ;count a character entered mov al, [ebx + ecx] ;move the first byte of the bcd value into al again. ;I can't remember for sure if this is legal, but you get ;the idea. and ax, 00FF ;clear out upper order of ax shr al, 4 ;get the next 4 bits into the low order push ax inc esi dec ecx .ENDW xor ecx, ecx ;make sure ecx is zero. xor ebx, ebx ;will be a running total now .WHILE(ecx < esi) ;need an incremented counter for arithmetic. .IF(ecx == 0) xor eax,eax ;zero out eax pop ax ;get the last value popped shl edx,16 ;high order moved to high order mov dx,ax ;low order add ebx,edx ;keep value .ELSE xor eax,eax ;zero eax again mov ax, 2 ;we're using base 2 imul cx ;multiply by the counter to get the place value (2^n). mov dx, ax ;save low order of multiplier (high order truncated) pop ax ;get next value imul dx ;multiply by multiplier shl edx,16 ;move high order of answer into high order of dx mov dx,ax ;similar for low order add ebx,edx ;add to running total. .ENDIF inc ecx ;increment counter. .ENDW mov eax,ebx ;return value ret binToChar endp
This obviously has some limitations, and I haven't tested it (I just wrote the code here on the forum) but everything looks fairly decent. Give it a try and let me know if it works?
#3
Posted 23 May 2010 - 08:57 AM
oops, remove that eax from the uses part. It's supposed to return a value, not preserve the register.
#4
Posted 24 May 2010 - 11:39 PM
#5
Posted 25 May 2010 - 04:08 AM
dargueta said:
Packed or unpacked BCD?
Well, the code I wrote works with packed, but doesn't read the sign bit at the end. That should be fairly straight forward though, just subtract 1 from the counter before jumping into the second loop, read the sign bit, and invert the number if necessary. Also, since packed BCD is a constant size, the size argument could be removed as well, and a constant number could be used for the loop. That would eliminate the need for two counter registers.
#6
Posted 28 May 2010 - 07:12 AM
that code is not relating what i asked.it is simply conversion of bcd number to binary.
#7
Posted 28 May 2010 - 09:43 AM
#8
Posted 28 May 2010 - 11:37 AM
gujjar said:
hi!
can any one help & give idea to write a program in MASM to convert BCD number to binary?
can any one help & give idea to write a program in MASM to convert BCD number to binary?
gujjar said:
that code is not relating what i asked.it is simply conversion of bcd number to binary.
Umm... Excuse me? You originally asked how to convert a BCD number to binary, I wrote code to convert BCD to binary. Then you proceeded to tell me that you didn't ask for code to convert BCD to binary.
AM I MISSING SOMETHING HERE!?!?!?!?
#9
Posted 28 May 2010 - 01:17 PM
Chill, dude. My guess is it's over gujjar's head and he/she thought it would be simpler than that. It isn't really. Binary to BCD is, but not the other way around.
sudo rm -rf /
#10
Posted 28 May 2010 - 02:27 PM
dargueta said:
Chill, dude. My guess is it's over gujjar's head and he/she thought it would be simpler than that. It isn't really. Binary to BCD is, but not the other way around.
I lawled at this. I was kidding around.
But for binary to BCD:
intasc32 proc NEAR C USES eax ebx ecx edx esi lpIn:DWORD, iNum:DWORD ;void intAsc32( strIn: char*, iNum: int); local minusOne:SDWORD local TEN:DWORD ;local constant for divison purposes. mov minusONE,-1 mov TEN,10 mov ebx,lpIn mov eax,iNum ;eax = number to convert xor ecx,ecx ;zero out counter. xor esi,esi mov edx,0 .WHILE (eax != 0) cdq ;convert to quad idiv TEN ;divide by ten to get each number place push dx ;store the remainder for later use inc ecx ;count a character entered. .ENDW @@stLoop: pop dx ;the digits are stored on the stack at this point. mov bx, dx ;save the high order digit inc esi .IF(esi == ecx) mov[ebx + esi],bl jmp @@done .ENDIF shl bx, 4 ;get the high order digit into high order pop dx mov bl,dl mov [ebx + esi],bx ;Just move them into your variable in memory. inc esi ;characters that ecx counted in the earlier code. cmp esi,ecx jne @@stLoop @@done: ret intasc32 endp
That's packed BCD (again without taking a negative sign into account). There's a problem with the pointer arithmetic in the mov [ebx + esi] line, but this is just some code I had laying around to work for ascii that I modified really fast, so I don't really feel like it right now. I just got done coding for 8 hours.
#11
Posted 28 May 2010 - 02:57 PM
Ah, crap. When I said "trivial" I thought that there was this certain instruction in the Intel ISA that did BCD to binary in a roundabout way, but it just allows you to do regular binary addition on a BCD int and get the right BCD answer.
sudo rm -rf /
#12
Posted 31 May 2010 - 08:57 AM
Yeah its true its complicate but if we willing to learn we can do it we can find answers to our questions
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









