Author Topic: the local var can't be setted.  (Read 911 times)

six_L

  • Member
  • **
  • Posts: 141
the local var can't be setted.
« on: December 28, 2017, 02:06:58 PM »
hi all.
the error has been happened on the folllowing codes. the local var can't be setted.
Code: [Select]
option casemap:none
option win64:7
option frame:auto

include \UASM64\include\windows.inc

includelib \UASM64\Lib\user32.lib
includelib \UASM64\Lib\kernel32.lib

ICO_MAIN equ 1000
DLG_MAIN equ 100
IDC_CLEAR equ 101
IDC_HEXDUMP equ 102
IDC_OUTPUT equ 103
IDC_INPUT equ 104

.data?
hInstance dq ?
hWinMain dq ?

.data
pShowStr db 1024 dup(0)

.code

HexAsciiDump proc uses rbx rsi rdi pDataBufIn:QWORD,dqDataSizeIn:QWORD,p6xDataBuf_out:QWORD
LOCAL temp[4]:BYTE
LOCAL _EndAddr:QWORD
local @szBufTmp[17]:BYTE
LOCAL dqRemainder:QWORD

mov rax,pDataBufIn
mov rcx,dqDataSizeIn
add rax,rcx
mov _EndAddr,rax
mov rsi, pDataBufIn
mov rcx, dqDataSizeIn
test rcx, rcx
jnz rowLoop
ret

hex_table:

db "000102030405060708090A0B0C0D0E0F"
db "101112131415161718191A1B1C1D1E1F"
db "202122232425262728292A2B2C2D2E2F"
db "303132333435363738393A3B3C3D3E3F"
db "404142434445464748494A4B4C4D4E4F"
db "505152535455565758595A5B5C5D5E5F"
db "606162636465666768696A6B6C6D6E6F"
db "707172737475767778797A7B7C7D7E7F"
db "808182838485868788898A8B8C8D8E8F"
db "909192939495969798999A9B9C9D9E9F"
db "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
db "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
db "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
db "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
db "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
db "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"

ascii_table:

db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db " ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/"
db "0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?"
db "@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"
db "P","Q","R","S","T","U","V","W","X","Y","Z","[","\","]","^","_"
db "`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"
db "p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."

rowLoop:
xor rdx,rdx
mov rax,dqDataSizeIn
mov rcx,16
div rcx
;int 3
mov dqRemainder,rdx ;here dqRemainder=3

rowLoop1:

invoke RtlZeroMemory,ADDR @szBufTmp, sizeof @szBufTmp
mov rax, rsi
sub rax, pDataBufIn
invoke wsprintf,addr @szBufTmp,CStr("%016IX"),rax

invoke lstrcat,p6xDataBuf_out,addr @szBufTmp
invoke lstrcat,p6xDataBuf_out,CStr(":")
invoke lstrcat,p6xDataBuf_out,CStr(" ")

invoke RtlZeroMemory, addr temp, sizeof temp

lea rdi, hex_table
push rsi
REPEAT 8
invoke lstrcat,p6xDataBuf_out,CStr(" ")
movzx rbx, BYTE PTR [rsi]
movzx rax, BYTE PTR [rdi+rbx*2]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
movzx rax, BYTE PTR [rdi+rbx*2+1]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
inc rsi

cmp rsi,_EndAddr
jz @exit_1

ENDM

invoke lstrcat,p6xDataBuf_out,CStr(" - ")

REPEAT 8
movzx rbx, BYTE PTR [rsi]
movzx rax, BYTE PTR [rdi+rbx*2]
lea rcx,temp
mov [rcx], rax
invoke lstrcat,p6xDataBuf_out,addr temp
movzx rax, BYTE PTR [rdi+rbx*2+1]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
invoke lstrcat,p6xDataBuf_out,CStr(" ")
inc rsi

cmp rsi,_EndAddr
jz @exit_1

ENDM
jmp _next
@exit_1:
;int 3
mov rax,dqRemainder ;here dqRemainder=0
cmp rax,0
jz _next

mov rax,16
sub rax,dqRemainder
xor rdx,rdx
mov rcx,3
mul rcx
mov rsi,rax
.if rsi < 24
add rsi,3 ;" - "
.endif
.repeat
invoke lstrcat,p6xDataBuf_out,CStr(" ")
dec rsi
.until rsi==0
_next:
pop rsi

lea rdi, ascii_table
invoke lstrcat,p6xDataBuf_out,CStr(" ")
REPEAT 16
movzx rbx, BYTE PTR [rsi]
movzx rax, BYTE PTR [rdi+rbx]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
inc rsi

cmp rsi,_EndAddr
jz @exit_2

ENDM
invoke lstrcat,p6xDataBuf_out,CStr(13,10)
jmp rowLoop1

@exit_2:
ret

HexAsciiDump endp

_ProcDlgMain proc hWnd:qword,wMsg:dword,wParam:qword,lParam:qword
local dqMemory0:QWORD
local dqStrSize:QWORD
local dqMsize:QWORD

mov eax,wMsg
.if eax == WM_INITDIALOG
push hWnd
pop hWinMain
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax

.elseif eax == WM_COMMAND
mov rax,wParam
.if ax == IDC_CLEAR
invoke SetDlgItemText,hWnd,IDC_INPUT,NULL

.elseif ax == IDC_HEXDUMP
invoke RtlZeroMemory, addr pShowStr, sizeof pShowStr
invoke GetDlgItemText,hWnd,IDC_INPUT,addr pShowStr,sizeof pShowStr
mov dqStrSize,rax
xor rdx,rdx
mov rcx,16
div rcx
inc rax
mov rcx,86
mul rcx
mov dqMsize,rax
invoke VirtualAlloc, NULL, dqMsize, MEM_COMMIT or MEM_RESERVE, PAGE_READWRITE 
or rax, rax
jz @ErrExit
mov dqMemory0, rax

invoke HexAsciiDump,addr pShowStr,dqStrSize,dqMemory0

invoke SetDlgItemText,hWnd,IDC_OUTPUT,NULL
invoke SetDlgItemText,hWnd,IDC_OUTPUT,dqMemory0

invoke VirtualFree, dqMemory0, dqMsize, MEM_DECOMMIT or MEM_RELEASE
@ErrExit:
.endif
.elseif eax == WM_CLOSE
invoke EndDialog,hWnd,NULL
.else
mov rax,FALSE
ret
.endif
mov rax,TRUE
ret

_ProcDlgMain endp

start Proc
invoke GetModuleHandle,NULL
mov hInstance,rax
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
start Endp

end
Code: [Select]
#include <\UASM64\include\resource.h>

#define ICO_MAIN 1000
#define DLG_MAIN 100
#define IDC_CLEAR 101
#define IDC_HEXDUMP 102
#define IDC_OUTPUT 103
#define IDC_INPUT 104

ICO_MAIN ICON "Main.ico"

DLG_MAIN DIALOG 0, 0, 337, 188
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "Test_HexDump"
FONT 10, "Calibri"
{
 EDITTEXT IDC_INPUT, 2, 2, 332, 40,ES_MULTILINE | ES_AUTOVSCROLL | WS_BORDER | WS_TABSTOP | WS_VSCROLL
 EDITTEXT IDC_OUTPUT, 2, 60, 332, 125,ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_BORDER | WS_TABSTOP | WS_VSCROLL
 PUSHBUTTON "Clear(&C)", IDC_CLEAR,242, 44, 45, 14
 PUSHBUTTON "HexDump(&D)", IDC_HEXDUMP, 288, 44, 45, 14, WS_TABSTOP
}


AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: the local var can't be setted.
« Reply #1 on: December 28, 2017, 10:00:58 PM »
I don't know if it is the real reason because I have not tested, but you must not use push and pop in 64-bit, in particular before calling an external function.

six_L

  • Member
  • **
  • Posts: 141
Re: the local var can't be setted.
« Reply #2 on: December 29, 2017, 02:17:15 AM »
hi,aw27
thanks your respone.
i used your method,but the error is still there.
Code: [Select]
1.
local @rsi:qword
 
mov @rsi,rsi
...
mov rsi,@rsi
2.
uses r12...
mov r12,rsi
...
mov rsi,r12

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5773
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: the local var can't be setted.
« Reply #3 on: December 29, 2017, 02:46:31 AM »
Jose is correct here, mess up alignment in win64 and the app will exit telling you nothing. If you must preserve a register, use a LOCAL. Now the general idea is to keep removing problems and when you get it clean and tidy you have a better chance of finding what is wrong if the app does not work.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

six_L

  • Member
  • **
  • Posts: 141
Re: the local var can't be setted.
« Reply #4 on: December 29, 2017, 03:23:50 AM »
hi,hutch--
thanks your respone.
Quote
Now the general idea is to keep removing problems and when you get it clean and tidy you have a better chance of finding what is wrong if the app does not work.
I agree entirely.
the problem seems a bit strange. i used the "dqRemainder" var as a local, the app can't work rightly. and used the "dqRemainder" var as a global, the app is crashed.

six_L

  • Member
  • **
  • Posts: 141
Re: the local var can't be setted.
« Reply #5 on: December 29, 2017, 08:50:10 PM »
hi,all
Quote
must not use push and pop in 64-bit
this is causing the strange problem.
here can't use the "uses rbx rsi rdi r12"
replace:
   mov   @rbx,rbx
   mov   @rsi,rsi
   mov   @rdi,rdi
   mov   @r12,r12
...
   mov   rbx ,@rbx
   mov   rsi ,@rsi
   mov   rdi ,@rdi
   mov   r12 ,@r12

        ret

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: the local var can't be setted.
« Reply #6 on: December 29, 2017, 09:22:08 PM »
I did not say you could not use pushes and pops at all. I meant you you should not use them outside the prologue , particularly when calling functions.

In your example I believe the problem is here:
LOCAL temp[4]:BYTE
replace with
LOCAL temp[8]:BYTE
and it may fix (or may not).

 

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5773
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: the local var can't be setted.
« Reply #7 on: December 29, 2017, 10:06:19 PM »
In 64 bit, forget "USES" if it uses PUSH/POP at all. You are safe if the procedure is aligned in the first place if you create locals in the normal manner, copy the regs to the locals, write the code that uses the registers to the locals and when finished, write the locals back to the registers. This is MASM notation but its logic is the same as UASM.

whatever proc args etc ....

LOCAL @15 :QWORD
LOCAL @14 :QWORD
LOCAL @13 :QWORD
LOCAL @12 :QWORD

mov @15, r15
mov @14, r14
mov @13, r13
mov @12, r12

; write the code using these regs here

mov r15, @15
mov r14, @14
mov r13, @13
mov r12, @12

ret

whatever endp

Note that with the regs, you don't have to use reverse order like PUSH/POP as it is not a last on, first off order like the stack. Code like this is safe and reliable.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

AW

  • Member
  • *****
  • Posts: 1490
  • Let's Make ASM Great Again!
Re: the local var can't be setted.
« Reply #8 on: December 29, 2017, 10:36:57 PM »
In 64 bit, forget "USES" if it uses PUSH/POP at all.
The guys that developed MASM 64-bit use indeed PUSHes and POPs for USES but they don't bother to align the stack in the end, we need to do that ourselves.
UASM goes one step further and does align (sometimes we find cases where it does not but that is another story).

For MASM 64-bit we can always use Steve's STACKFRAME Macro and start living in a world without pushes and pops.


six_L

  • Member
  • **
  • Posts: 141
Re: the local var can't be setted.
« Reply #9 on: December 29, 2017, 11:18:31 PM »
hi, all
Quote
LOCAL temp[4]:BYTE
replace with
LOCAL temp[8]:BYTE
it‘s fixed.
/////////////////
the folllowing codes is worked.
Code: [Select]
HexAsciiDump proc uses rbx rsi rdi pDataBufIn:QWORD,dqDataSizeIn:QWORD,p6xDataBuf_out:QWORD
LOCAL temp[8]:BYTE
LOCAL _EndAddr:QWORD
local @szBufTmp[17]:BYTE
LOCAL dqRemainder:QWORD

mov rax,pDataBufIn
mov rcx,dqDataSizeIn
add rax,rcx
mov _EndAddr,rax
mov rsi, pDataBufIn
mov rcx, dqDataSizeIn
test rcx, rcx
jnz rowLoop
ret

hex_table:

db "000102030405060708090A0B0C0D0E0F"
db "101112131415161718191A1B1C1D1E1F"
db "202122232425262728292A2B2C2D2E2F"
db "303132333435363738393A3B3C3D3E3F"
db "404142434445464748494A4B4C4D4E4F"
db "505152535455565758595A5B5C5D5E5F"
db "606162636465666768696A6B6C6D6E6F"
db "707172737475767778797A7B7C7D7E7F"
db "808182838485868788898A8B8C8D8E8F"
db "909192939495969798999A9B9C9D9E9F"
db "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
db "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
db "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
db "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
db "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
db "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"

ascii_table:

db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db " ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/"
db "0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?"
db "@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"
db "P","Q","R","S","T","U","V","W","X","Y","Z","[","\","]","^","_"
db "`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"
db "p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."
db ".",".",".",".",".",".",".",".",".",".",".",".",".",".",".","."

rowLoop:
xor rdx,rdx
mov rax,dqDataSizeIn
mov rcx,16
div rcx
;int 3
mov dqRemainder,rdx

rowLoop1:

invoke RtlZeroMemory,ADDR @szBufTmp, sizeof @szBufTmp
mov rax, rsi
sub rax, pDataBufIn
invoke wsprintf,addr @szBufTmp,CStr("%016IX"),rax

invoke lstrcat,p6xDataBuf_out,addr @szBufTmp
invoke lstrcat,p6xDataBuf_out,CStr(":")
invoke lstrcat,p6xDataBuf_out,CStr(" ")

invoke RtlZeroMemory, addr temp, sizeof temp

lea rdi, hex_table
push rsi
REPEAT 8
invoke lstrcat,p6xDataBuf_out,CStr(" ")
movzx rbx, BYTE PTR [rsi]
movzx rax, BYTE PTR [rdi+rbx*2]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
movzx rax, BYTE PTR [rdi+rbx*2+1]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
inc rsi

cmp rsi,_EndAddr
jz @exit_1

ENDM

invoke lstrcat,p6xDataBuf_out,CStr(" - ")

REPEAT 8
movzx rbx, BYTE PTR [rsi]
movzx rax, BYTE PTR [rdi+rbx*2]
lea rcx,temp
mov [rcx], rax
invoke lstrcat,p6xDataBuf_out,addr temp
movzx rax, BYTE PTR [rdi+rbx*2+1]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
invoke lstrcat,p6xDataBuf_out,CStr(" ")
inc rsi

cmp rsi,_EndAddr
jz @exit_1

ENDM
jmp _next
@exit_1:
;int 3
mov rax,dqRemainder
cmp rax,0
jz _next

mov rax,16
sub rax,dqRemainder
xor rdx,rdx
mov rcx,3
mul rcx
mov rsi,rax

.if rsi > 24
add rsi,3 ;" - "
.endif
.repeat
invoke lstrcat,p6xDataBuf_out,CStr(" ")
dec rsi
.until rsi==0
_next:
pop rsi

lea rdi, ascii_table
invoke lstrcat,p6xDataBuf_out,CStr(" ")
REPEAT 16
movzx rbx, BYTE PTR [rsi]
movzx rax, BYTE PTR [rdi+rbx]
lea rcx,temp
mov [rcx], al
invoke lstrcat,p6xDataBuf_out,addr temp
inc rsi

cmp rsi,_EndAddr
jz @exit_2

ENDM
invoke lstrcat,p6xDataBuf_out,CStr(13,10)
jmp rowLoop1

@exit_2:

ret

HexAsciiDump endp