首页
社区
课程
招聘
[求助]谁有hex2dec的代码
发表于: 2009-6-6 21:57 6815

[求助]谁有hex2dec的代码

2009-6-6 21:57
6815
收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 2096
活跃值: (100)
能力值: (RANK:420 )
在线值:
发帖
回帖
粉丝
2
請問你是不是跑錯間了?
這裏是 密碼學版。
 /* 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

上传的附件:
2009-6-6 22:12
0
雪    币: 419
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
谢谢,有人回答的板块都是好版块 嘿嘿
2009-6-6 22:16
0
雪    币: 419
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
google

了下

mov     ax,@data 不知道这是什么汇编的写法,具体是什么意思

Hi Blip,

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.
2009-6-6 22:42
0
雪    币: 1450
活跃值: (35)
能力值: (RANK:680 )
在线值:
发帖
回帖
粉丝
5
这贴该发到编程区~
2009-6-6 23:56
0
雪    币: 164
活跃值: (10)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
6
@data 就是.data的地址
2009-6-7 13:45
0
游客
登录 | 注册 方可回帖
返回
//