/* hex2dec.c
* Language: C
* License: Unknown
* Copyright: (C) 1998
* LOC: 22
* hex2dec.c
*
* Convert decimal integers into hexadecimal
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
*
* The Initial Developer of the Original Code is
* Michael J. Fromberger.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* $Id: hex2dec.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char *argv[])
{
mp_int a;
char *buf;
int len;
if(argc < 2) {
fprintf(stderr, "Usage: %s <a>\n", argv[0]);
return 1;
}
mp_init(&a); mp_read_radix(&a, argv[1], 16);
len = mp_radix_size(&a, 10);
buf = malloc(len);
mp_toradix(&a, buf, 10);
printf("%s\n", buf);
free(buf);
mp_clear(&a);
return 0;
}
; File Name : hex2dec.asm
; Processor : x86
; Operating System : DOS
; Function : Convert max. 2 digits Hexadecimal to Decimal Number
; Make Date : 2001
; Revision Date : 04 December 2004
; Assembler : Turbo Assembler Version 2.0
; By : l411v
; l411v@yahoo.com
; www.l411v.com
;################################################################ M A C R O ###
cetak_str macro str
lea dx,str
call csa
endm
input_str macro str
lea dx,str
call is
endm
;#################################################### D E C L A R A T I O N ###
code segment
assume cs:code
org 100h
deklarasi:
jmp mulai
kata1 db 'Input Hexadecimal Number [max. 2 digit] : $'
kata2 db 13,10,'The Decimal : $'
hex_max db 3
hex_len db ?
hex_arr db 3 dup(?)
;################################################################ S T A R T ###
mulai:
mov ax,3
int 10h
cetak_str kata1
input_str hex_max
call banding
mov ax,0
mov bx,0
cmp hex_len,1
je kecil
besar:
mov al,hex_arr[bx]
mov cl,4
shl al,cl
inc bx
kecil:
add al,hex_arr[bx]
push ax
cetak_str kata2
pop ax
call bagi_hasil
exit2dos:
mov ah,4ch
int 21h
;######################################################## P R O C E D U R E ###
csa proc
mov ah,9
int 21h
ret
csa endp
is proc
mov ah,0ah
int 21h
ret
is endp
banding proc
mov bx,0
mov cl,hex_len
mov ch,0
ulang1:
mov al,hex_arr[bx]
sub al,30h
cmp al,9
jbe lanjut
sub al,11h
cmp al,5
jbe lanjut1
sub al,20h
cmp al,5
jbe lanjut1
jmp mulai
lanjut1:
add al,10
lanjut:
mov hex_arr[bx],al
inc bx
loop ulang1
ret
banding endp
bagi_hasil proc
mov dx,0ffffh
push dx
ulang2:
mov dl,10
div dl
push ax
mov ah,0
cmp al,0
je hasil
jmp ulang2
hasil:
pop ax
cmp ax,0ffffh
je exit2dos
mov al,ah
add al,30h
int 29h
jmp hasil
ret
bagi_hasil endp
code ends
end deklarasi
; EOF-l411v
Do you mean for example 0xffffffff(hex) ==> 4294967295(dec) and displaying heximal in its correct decimal value ??
I'm not sure what your intentions are but here's a little simple peace of code I've ripped off from one of my old (unfinished) source codes.
This little sub routine works fine.
It translates hex values into real BCD values (longwords).
I have used this little code to present realtime mouse coords on screen in video mode (that was the purpose why I coded it anyway).
I guess you probably want to do something similar like that.
I assume you may want to add this code to your project(s).
Notice also that no code setup has been made here since this little code was ment to be only a sub-routine.
I assume you're familiar with setup stuff like ".code , .data , .386 , .model , jumps , end<entry point>" etc. so I didn't bother modifying the source for you further (Sorry my friend but I'm a very lazy coder).
Well ok then, here's the source...
;*********************************************************
;****** Computing hexadecimal to decimal digits. ******
;****** eax= Quotient, ebx = Divident / Multiplier. ******
;****** ecx = counter, edx = Remainder. ******
;****** Digits (0-9) will be stored ASCII wise. ******
;*********************************************************
Hex2Dec:sub eax,eax ;Initializing..
mov edx,eax
mov ax,@data
mov ds,ax
mov eax,HexInput ;Input: y=???.
mov ebx,10 ;Base 10.
mov cx,9 ;Digit counter.
HexLoop:div ebx ;Formula: x=(y/10).
add edx,30h ;Adjust BCD to Ascii.
mov si,cx
mov [DecDigits+si*1],dl
xor edx,edx ;Reset remainder.
dec cx ;Decrement counter.
jnz hexloop ;Now go see next digit.
ret ;Done !
.data
HexInput dd 0FFFFFFFFh ;Input: <max 32 bits>.
DecDigits db 10 dup (0) ;Temp storage of 10 decimal digits.