News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

converting DWTOA to the GoAsm Syntax

Started by shankle, June 22, 2012, 06:09:18 AM

Previous topic - Next topic

shankle

Thanks for any help.

SaveNumber  db   0
holdp             db   "   ",0
BufAdd          dd   0

dwtoa            PROTO :DWORD, lpBuffer: PTR BYTE

; MY CODE...
;   edx is the input field
;   ebx  holdp is the output field   
       push ebx
       lea ebx, holdp               ; convert number
       inc ebx                      ; save 1st byte for # sign
       xor edx, edx
       mov dl, SaveNumber           ; filled in other parts of program
       mov BufAdd, ebx
       invoke dwtoa, edx, BufAdd    ; Hex DD to string                                                                     
; End My Code.....
                                   
; Code from the MASM32 Forum
dwtoa proc public uses esi edi dwValue:DWORD, lpBuffer:PTR BYTE
   
    ; -------------------------------------------------------------
    ; convert DWORD to ascii string
    ; dwValue is value to be converted
    ; lpBuffer is the address of the receiving buffer
    ; EXAMPLE:
    ; invoke dwtoa,edx,addr buffer
    ;
    ; Uses: eax, ecx, edx.
    ; -------------------------------------------------------------

    mov   eax, dwValue
    mov edi, [lpBuffer]

    ; Is the value negative?

      .if   (sdword ptr eax < 0)
       mov byte ptr [edi], '-'   ; store a minus sign
       inc edi
       neg eax         ; and invert the value
      .endif

    mov esi, edi      ; save pointer to first digit

    mov ecx, 10
    .while (eax > 0)      ; while there is more to convert...
      xor edx, edx
      div ecx         ; put next digit in edx
      add dl, '0'      ; convert to ASCII
      mov [edi], dl      ; store it
      inc edi
    .endw

    mov byte ptr [edi], 0   ; terminate the string

    ; We now have all the digits, but in reverse order.

    .while (esi < edi)
      dec edi
      mov al, [esi]
      mov ah, [edi]
      mov [edi], al
      mov [esi], ah
      inc esi
    .endw   
    ret
dwtoa endp
; -----------------------------------
; -----------------------------------

; Here is the mess I made of it
; It is a work in progress

SaveNumber  db  0
holdp             db  '   ',0
BufAdd           dd  0
DwValue        dd 0

   invoke dwtoa                   ; Hex DD to string
   
; My Code
;   edx is the input field
;   ebx  holdp is the output field   
       push ebx
       lea ebx,[holdp]               ; convert number
       inc ebx                       ; save 1st byte for # sign
       xor edx,edx
       mov dl, [SaveNumber]          ; input
       mov [BufAdd],ebx
       invoke dwtoa                   ; Hex DD to string
; End My Code

; Note - do I need to pass parameters here by pushing them on the
; stack???
dwtoa:
;     FRAME public uses esi edi dwValue:DWORD, lpBuffer:PTR BYTE
   
    ; -------------------------------------------------------------
    ; convert DWORD to ascii string
    ; dwValue is value to be converted
    ; lpBuffer is the address of the receiving buffer
    ; EXAMPLE:
    ; invoke dwtoa,edx,addr buffer
    ;
    ; Uses: eax, ecx, edx.
    ; -------------------------------------------------------------
    xor eax,eax
    mov al,[SaveNumber]
    mov [dwValue],eax
    mov edi,[BufAdd]

    ; Is the value negative?

;      .if   (sdword ptr eax < 0)
??   cmp ADDR [dwValue],< 0
      jnl >n60
      mov B[edi], '-'    ; store a minus sign
      inc edi
      neg eax       ; and invert the value
;    .endif
n60:
     mov esi,edi      ; save pointer to first digit

     mov ecx,10
;    .while (eax > 0)    ; while there is more to convert...
doitagn:
     cmp eax,0         ; while there is more to convert...
      jle >n61     
      xor edx,edx
      div ecx       ; put next digit in edx
      add dl,0'          ;  convert to ASCII
      mov [edi],dl    ; store it
      inc edi
      jmp doitagn
;    .endw
n61:
;    mov byte ptr [edi], 0   ; terminate the string
     mov B[edi], 0          ; terminate the string

    ; We now have all the digits, but in reverse order.

;    .while (esi < edi)
doitagn2:
??    cmp (esi < edi)
      jge >n62
      dec edi
      mov al,[esi]
      mov ah,[edi]
      mov [edi],al
      mov [esi],ah
      inc esi
      jmp doitagn2
;    .endw
n62:   
    ret   
;dwtoa endf




satpro

shankle,

This may help.  I can't remember where I got them, and wish I could, but they are in GoAsm format--ready to go.

Good luck,
Bert

satpro

P.S,
Yes on the "do I pass parameters".
invoke dwtoa, [dwValue], ADDR lpBuffer
'or'
push ADDR lpBuffer, [dwValue]
call dwtoa

Something like that.  Passing a register instead of memory for dwValue saves time & space.  In the zip the routine is called "dw2ascii".  Convert.asm describes 'em all.

shankle

Thanks Satpro.
Will download it and check it out.

Does your name imply that you dabble in BUDs?
I have a 10 footer and am heavily into FTA.
Can't stand Dish and the other one.

wjr

You were getting close. This would be a cleaned up version of dwtoa using my preference for local labels within a procedure. Note the changes to FRAME...ENDF and your ?? conditional jumps.


dwtoa:
    FRAME dwValue, lpBuffer
    USES esi,edi
   
    ; -------------------------------------------------------------
    ; convert DWORD to ascii string
    ; dwValue is value to be converted
    ; lpBuffer is the address of the receiving buffer
    ; EXAMPLE:
    ; invoke dwtoa,edx,addr buffer
    ;
    ; Uses: eax, ecx, edx.
    ; -------------------------------------------------------------

    mov eax,[dwValue]
    mov edi,[lpBuffer]
    test eax,eax     ; Is the value negative?
    jns >

    mov B[edi],'-'   ; store a minus sign
    inc edi
    neg eax          ; and invert the value
:
    mov esi,edi      ; save pointer to first digit
    mov ecx,10
.convert
    test eax,eax     ; while there is more to convert...
    jz >

    xor edx,edx
    div ecx          ; put next digit in edx
    add dl,'0'       ; convert to ASCII
    mov [edi],dl     ; store it
    inc edi
    jmp <.convert
:
    mov B[edi], 0    ; terminate the string
.reverse             ; We now have all the digits, but in reverse order
    cmp esi,edi
    jae >

    dec edi
    mov al,[esi]
    mov ah,[edi]
    mov [edi],al
    mov [esi],ah
    inc esi
    jmp <.reverse
:   
    ret   
    ENDF


Although it looks like you were trying to use global variables to avoid doing this, using the above would involve pushing the parameters on the stack or using invoke as follows:


    DATA
SaveNumber  db  0
holdp       db  '    ',0

    CODE
    movsx edx, B[SaveNumber]          ; input
    invoke dwtoa, edx, ADDR holdp     ; Hex DD to string


It does look like you were expecting signed bytes, so I made the holdp buffer a bit larger. Also, to detect those signed bytes, you can use movsx to sign extend the byte into a dword register.

satpro

Quote from: shankle on June 22, 2012, 11:15:37 AM
Thanks Satpro.
Can't stand Dish and the other one.
No problem...
Oops, had a DISH (and the other one) dealership for 14 years.  Dropped it to campaign for Obama in 2008.  Never went back to work.  We had saved and were tired anyway.  Now all my TV comes through the back door, which is great, because you can't beat free and we LOVE Australian movies and British TV (well, not the sitcoms so much) and you can't really get that on American TV.  Got a Big-screen Sony and desktop HP connected to a 2 TB wireless network drive.  The whole house watches whatever they want--no commercials.  My big job every morning is getting last night's TV.

Yeah, I dabble in buds... but not the satellite so much anymore 8)
Bert

shankle

Thank you much WJR.
Atodw/dwtoa now compile without causing errors.
God am I green at this........
Next hurdles - haven't filled out the Wndclassex struct,  probably need to follow
the same procedure with others frames as in the dwtoa example,  I have simply
named the labels Create, Character Destroy, Paint not defining them anywhere.
Only example I have is "helloworld 3".
I have tried to use EQUs instead of some library.
I can get a clean 32-bit  compile now but the linker is having fits.
Couple of dozens files like GetModuleHandlea, hInstance, GetcommandLine
that the linker says I have not defined 

Never could get AdaptASm to work. Took out /h and still no luck.