The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: UtillMasm on May 05, 2009, 04:39:13 PM

Title: how to number to number?
Post by: UtillMasm on May 05, 2009, 04:39:13 PM
how to number to number?

example binToDec, hexToAsscii... :red
Title: Re: how to number to number?
Post by: dedndave on May 05, 2009, 06:17:22 PM
bin2hex
hex2asc

is this what you mean ? :eek
that was an easy one

try to tell us more about what you want to do  :U

an oldie but a goodie.......

        ADD     AL,90h
        DAA
        ADC     AL,40h
        DAA

AL register

hex    hex      ascii
in       out      out
0        30       0
1        31       1
2        32       2
3        33       3
4        34       4
5        35       5
6        36       6
7        37       7
8        38       8
9        39       9
A        41       A
B        42       B
C        43       C
D        44       D
E        45       E
F        46       F
Title: Re: how to number to number?
Post by: UtillMasm on May 05, 2009, 06:41:33 PM
 :U

i'm in thinking about my question. :wink
Title: Re: how to number to number?
Post by: UtillMasm on May 05, 2009, 06:42:37 PM
maybe this one:

.586
.model flat,stdcall
include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
.data
szOutTextBuf db 256 dup(0)
.code
ReverseStr proc uses esi edi lpStr
  xor eax,eax
  .if !lpStr
   ret
  .endif
  mov eax,lpStr
  xor esi,esi
  xor edi,edi
  .while BYTE ptr [eax + esi]
   inc esi
  .endw
  .while esi != edi
   mov cl,[eax+esi-1]
   mov dl,[eax+edi]
   mov BYTE ptr [eax+edi],cl
   mov BYTE ptr [eax+esi-1],dl
   inc edi
   dec esi
  .endw
  ret
ReverseStr endp
dwtos proc uses ebx lpOutBuff,dwValue,nType
  ;lpOutBuff
   ;为输出缓冲区
  ;dwValue
   ;为要转换的整数值
  ;nType
   ;参数是转换类型
   ;分别可以是2、8、10、16进制都可以转换
  xor eax,eax
  .if !(lpOutBuff)
   ret
  .endif
  mov ebx,lpOutBuff
  mov ecx,nType
  mov eax,dwValue
  .while eax > ecx || eax
   xor edx,edx
   div ecx
   .if dl > 9
    add dl,'A' - 10
   .else
    add dl,'0'
   .endif
   mov byte ptr [ebx],dl
   inc ebx
  .endw
  invoke ReverseStr,lpOutBuff
  ret
dwtos endp
start:
invoke dwtos,offset szOutTextBuf,12345678,10
  ;示例代码
invoke MessageBox,0,eax,NULL,MB_OK
invoke ExitProcess,NULL
ret
end star

@if exist Number.obj del Number.obj
@set include=\masm32\include
@\masm32\bin\ml.exe /c /coff /Cp /FoNumber.obj /nologo Number.asm
@if exist Number.exe del Number.exe
@set lib=\masm32\lib
@\masm32\bin\link.exe /subsystem:windows /out:Number.exe /nologo Number.obj
@pause


or maybe something else?

i'm go back to my thinking. :wink
Title: Re: how to number to number?
Post by: dedndave on May 05, 2009, 07:00:17 PM
try it !!!! - very cool   :8)

        ADD     AL,90h
        DAA
        ADC     AL,40h
        DAA
Title: Re: how to number to number?
Post by: brethren on May 05, 2009, 08:43:39 PM
Quote from: dedndave on May 05, 2009, 07:00:17 PM
try it !!!! - very cool   :8)

        ADD     AL,90h
        DAA
        ADC     AL,40h
        DAA

i'm currently working on my own library, as a starting point i'm basically reimplementing parts of the c std lib using asm to make a more effecient/faster version. The last proc i implemented was a port of k+r's itoa function from here http://en.wikipedia.org/wiki/Itoa#K.26R_implementation

anyway if any of you guru's have any more cool asm snippets that'd help me with my project please post 'em

thanks
Title: Re: how to number to number?
Post by: Mark Jones on May 05, 2009, 08:59:06 PM
In other words, thanks Dave! :bg

Yes that's interesting. Small, too.
Title: Re: how to number to number?
Post by: dedndave on May 05, 2009, 10:32:13 PM
well - i have to be honest, here - it was not my original code
it was used in the BIOS of the IBM PC and PC/XT to display memory test addresses at boot (remember how anoyingly long that took?)
but, i have used that one several times - lol
btw - if i remember, the AH register gets wiped out

i'd like to add that, this little strip of code is my all-time favorite
i always keep it in the back of my head when i am trying to find a slick way to do something
it has inspired me to devise a few neat ones
Title: Re: how to number to number?
Post by: dedndave on May 05, 2009, 11:42:16 PM
here's another w.o.o.i.t (worn-out old indian trick) from the old days.....

let's say you have a value in AX (or AL or EAX, etc)
you want to find the lowest order bit that is set
i.e.    for 01110100 - you want to isolate the lowest one - or - 00000100
at first glance, you want to use a single-bit TEST mask and shift it left until the TEST is true

like this.........

              MOV      DX,8000h
              MOV      CX,16

LOOP01:   ROL     DX,1
               TEST    AX,DX
               LOOPZ  LOOP01

but, there is an easier way....

    MOV     DX,AX
    DEC      AX
    XOR     AX,DX
    INC      AX
    SHR      AX,1

the key is that all the 0's under the first 1 bit become 1's with the DEC
the XOR gets rid of all the 1's above
the last 2 instructions clean up the mess
look ma! - no loop   :dance:
Title: Re: how to number to number?
Post by: MichaelW on May 06, 2009, 05:50:00 AM
This isn't from the old, old days, but it was supported on the 80386 starting in 1985:

    bsf cx, ax
    mov ax, 1
    shl ax, cl

Title: Re: how to number to number?
Post by: dedndave on May 06, 2009, 07:16:41 AM
that IS the old days - lol
you trying to avoid admiting it ?

i think the shl ax,cl kills it for time
it is short, though  :U