The MASM Forum

General => The Campus => Topic started by: MDWLibby on March 11, 2014, 05:54:06 AM

Title: Can use some help with this.
Post by: MDWLibby on March 11, 2014, 05:54:06 AM
I originally did the in Power Basic, (Inline Assembler) and had no trouble getting it  to work.  After making the needed adjustments, and trying everything I could find in the Docs.  I have come to realize what ever the problem is; it must be rather simple.  Can't see the trees through the forest.  This routine simply strips of bits 8 & 7 from each of the characters in a 5 character string and places the result in a 32 bit var.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                     Build this console app with
                  "MAKEIT.BAT" on the PROJECT menu.
        ----------------------------------------------------- *

  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
  ; My Proto Types ***
  ;--------------------------------------------------------------------------
  PackString PROTO :DWORD, :DWORD
  ;--------------------------------------------------------------------------

    .data?
      value dd ?        ;This was here
      pac1dd dd ?       ;Packed bits 0 - 31
      pac2dd dd ?       ;Packed bits 32 - 63

    .data
      item dd 0
      alpha db "alpha", 0

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
   
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    cls
    print "Hello World",13,10
    print "Just want to see if this will work.",13,10

    INVOKE PackString ,alpha ,pac1dd
   
    inkey "Press a key."
   
    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; Compress String XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

PackString proc pString:DWORD, Reg32:DWORD

    push ebx                ;Volitile must store *******************
    ;---------------------------------------------------------------
    xor eax, eax            ;Clear all bits
    xor ecx, ecx
    ;---------------------------------------------------------------
    mov ebx, pString        ;ptr of address of string     
    mov ebx, [ebx]          ;1st char. address of string
    mov al, [ebx + 1]       ;Char. 2 in al
    ;---------------------------------------------------------------
    and al, 63              ;Strip off bits 8 & 7
    shl al, 2               ;Shift left 2 places  1111-1100
    ;---------------------------------------------------------------
    mov cl, [ebx]           ;Char. 1 in cl
    ;---------------------------------------------------------------
    and cl, 63              ;Char. 1 in bits 5-0  0011-1111
    mov ah, cl              ;Char. 1 & 2 in AX  0011-1111-1111-1100
    shr ax, 2               ;AX = 0000-1111-1111-1111
    push ax                 ;Store Char. 1 & 2 Right Justifyed
    ;---------------------------------------------------------------
    xor eax, eax            ;Clear all bits
    xor ecx, ecx
    ;---------------------------------------------------------------
    mov al, [ebx + 3]       ;Char. 4 in al
    ;---------------------------------------------------------------
    and al, 63
    shl al, 2               ;Shift left 2 places  1111-1100
    ;---------------------------------------------------------------
    mov cl, [ebx + 2]       ;Char. 3 in cl
    ;---------------------------------------------------------------
    and cl, 63              ;Char. 3 in bits 5-0  0011-1111
    mov ah, cl              ;Char. 3 & 4 in AX  0011-1111-1111-1100
    shl ax, 2               ;AX = 1111-1111-1111-0000
    push ax                 ;Store Char. 3 & 4 Left Justified
    ;---------------------------------------------------------------
    pop eax                 ;eax = Characters 1, & 2 + 3, & 4
    shl eax, 4              ;Make last 8 bits avalible
                            ;Add Char. 5
    xor ecx, ecx            ;Clear ecx
    ;---------------------------------------------------------------
    mov cl, [ebx + 4]       ;Char. 5 in cl
    ;---------------------------------------------------------------
    and cl, 63              ;Char. 5 in bits 5-0 0011-1111
    shl cl, 2               ;Shift left 2 places 1111-1100
    mov al, cl              ;eax 11111111-11111111-11111111-11111100
    ;---------------------------------------------------------------
    mov Reg32, eax          ;Compressed String
    ;---------------------------------------------------------------
    pop ebx                 ;Volitile must restore *****************
    ;---------------------------------------------------------------
    ret
     
PackString endp
;--------------------------------------------------------------------------
end start
Title: Re: Can use some help with this.
Post by: GoneFishing on March 11, 2014, 06:10:08 AM
WerFault.exe reports:
Quote
Problem signature:
  Problem Event Name:   APPCRASH
  Application Name:   packstr.exe
  Application Version:   0.0.0.0
  Application Timestamp:   531e0bce
  Fault Module Name:   packstr.exe
  Fault Module Version:   0.0.0.0
  Fault Module Timestamp:   531e0bce
  Exception Code:   c0000005
  Exception Offset:   0000108f
Run your exe under  debugger and set the breakpoint at 40108f:
Quote
Breakpoint 0 hit
eax=00000000 ebx=00000061 ecx=00000000 edx=0018ff38 esi=00000000 edi=00000000
eip=0040108f esp=0018ff76 ebp=0018ff7a iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
image00400000+0x108f:
0040108f 8b1b            mov     ebx,dword ptr [ebx]  ds:002b:00000061=????????
0:000> da ebx
00000061  "????????????????????????????????"
Seems like ebx already contains first character
Title: Re: Can use some help with this.
Post by: MDWLibby on March 11, 2014, 06:41:08 AM
Yes you are correct.  But the first char. is not the Character I'm striping, its the 2nd.

    mov ebx, pString        ;ptr of address of string     
    mov ebx, [ebx]          ;1st char. address of string
    mov al, [ebx + 1]       ;Char. 2 in al    <-------------------------------<<

I forgot to mention the problem is when I run the program it crashes.  However it shows no errors when assembled.
Title: Re: Can use some help with this.
Post by: dedndave on March 11, 2014, 06:41:48 AM
seems like a lot of code   :P

;###############################################################################################

        INCLUDE    \Masm32\Include\Masm32rt.inc

;###############################################################################################

Pack5   PROTO   :LPSTR

;###############################################################################################

        .DATA

szAlpha db "alpha", 0

;***********************************************************************************************

;       .DATA?

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  Pack5,offset szAlpha
        print   uhex$(eax),13,10

        inkey
        INVOKE  ExitProcess,0

_main   ENDP

;***********************************************************************************************

Pack5   PROC    lpString:LPSTR

        mov     edx,lpString
        mov     ecx,5
        jmp short Loop01

Loop00: shl     eax,6
        inc     edx

Loop01: mov     al,[edx]
        shl     al,2
        dec     ecx
        jnz     Loop00

        ret

Pack5   ENDP

;###############################################################################################

        END     _main
Title: Re: Can use some help with this.
Post by: GoneFishing on March 11, 2014, 06:45:09 AM
Quote from: MDWLibby on March 11, 2014, 06:41:08 AM
Yes you are correct.  But the first char. is not the Character I'm striping, its the 2nd.

    mov ebx, pString        ;ptr of address of string check it, I think it's not a pointer but the first char - 61     
    mov ebx, [ebx]          ;1st char. address of string <----- APPCRASH here
    mov al, [ebx + 1]       ;Char. 2 in al    <-------------------------------<<

I forgot to mention the problem is when I run the program it crashes.  However it shows no errors when assembled.

Title: Re: Can use some help with this.
Post by: MDWLibby on March 11, 2014, 06:58:36 AM
Thanks;  and your right it is a lot, compared to what you came-up with.  However this is the 1st assm. Iv'e tried in 30 years so what I was aiming for was just to get it to run without crashing.  And learn in the process.  Your example helps, but I'm still not sure what I did wrong.
Title: Re: Can use some help with this.
Post by: GoneFishing on March 11, 2014, 07:25:11 AM
Quote from: MDWLibby on March 11, 2014, 06:58:36 AM
...
Your example helps, but I'm still not sure what I did wrong.

Quote
INVOKE PackString , offset  alpha ,  offset  pac1dd
 printf("%08X\n", eax) ; printf("%08X\n", pac1dd)  doesn't work here  :icon_confused:

Quote
mov ebx, pString        ;ptr of address of string     
   ; mov ebx, [ebx]          ;1st char. address of string comment out this line


HTH
Title: Re: Can use some help with this.
Post by: MDWLibby on March 11, 2014, 07:38:36 AM
Thanks; I knew it had to be something simple.  I tried using offset in the call but when it didn't work it didn't dawn on me that I was still referencing a pointer to a pointer.  Which in the PB prog. it is needed.  Well live and learn.
Thanks again.
Title: Re: Can use some help with this.
Post by: GoneFishing on March 11, 2014, 08:08:51 AM
here referencing a pointer to a pointer is needed:
Quote
mov edx, Reg32
mov [edx], eax          ;Compressed String
and now the line    printf("%08X\n",pac1dd) works OK

[EDIT]: Now I'm thinking if there's a way to unpack the string from Reg32
Title: Re: Can use some help with this.
Post by: MDWLibby on March 11, 2014, 08:30:52 AM
I have wrote a routine to unpack,  however it is in PB asm.  I'll try to get it posted.  As I wrote this has been primarily an exercise and it would be nice to see what anyone might write.  So give it a go if you'd like.
Title: Re: Can use some help with this.
Post by: GoneFishing on March 11, 2014, 08:36:55 AM
I'll better wait for Dave's post  ;)
Title: Re: Can use some help with this.
Post by: dedndave on March 11, 2014, 09:42:00 AM
in your original code, it looked like you meant to pass a pointer to a pointer to the string
then, you passed the string

we normally just pass a pointer
to unpack, just reverse the process
you can start with a copy of the same procedure
pass it a DWORD and a pointer to a buffer, then modify the loop to unpack
Title: Re: Can use some help with this.
Post by: GoneFishing on March 11, 2014, 10:12:26 AM
HERE IT IS :


;###############################################################################################

INCLUDE    \Masm32\Include\Masm32rt.inc

;###############################################################################################

UnPack5   PROTO   :DWORD

;###############################################################################################

        .DATA

Reg32   dd  86cc2884h
szAlpha db  6 dup(0) 
     

;###############################################################################################

        .CODE

;***********************************************************************************************

main   PROC

        INVOKE  UnPack5, Reg32
       
        print   offset szAlpha,13,10,0

        inkey
        INVOKE  ExitProcess,0

main   ENDP

;***********************************************************************************************

UnPack5 PROC      PackedStr:DWORD
       
        push      ebx
        mov       eax, PackedStr
        mov       edx, offset szAlpha + 4
        mov       ecx, 5
        jmp short Loop01

Loop00: shr       eax,6
        dec       edx

Loop01: mov       bh,1
        mov       bl,al
        shr       bx,2
        mov byte  ptr [edx],bl
        dec       ecx
        jnz       Loop00
        pop       ebx
        ret

UnPack5 ENDP

;###############################################################################################

        END     main


Title: Re: Can use some help with this.
Post by: dedndave on March 11, 2014, 11:26:23 AM
nice that you got it going   :t

but, i would write it so that you pass a DWORD and a pointer

UnPack5 PROTO :DWORD,:LPVOID

;
        INVOKE  UnPack5,Reg32,offset szAlpha
;

UnPack5 PROC dwVal:DWORD,lpBuf:LPVOID

        mov       eax, dwVal
        mov       edx, lpBuf
;
;
;