Win x64 Questions



Hi Guys,

I'm starting to get more into Windows programming, whilst porting my
compiler from *nix to Windows.

However I'm having a problem with the calling convention, or more
correctly I'm making a call to the OS, and it appears that the stack is
being trashed or I'm just calling the OS wrong?

So any clues on what's going on would be appreciated. (Code is at the
bottom and requires FASM).

Running it through a debugger, shows that on return from the call to
WriteFile() that the stack isn't quite correct and on return from my
function that calls WriteFile() is jumping to 0...

PS. It's a console application that simple prints 2 string to stdout.

--
Darran (aka Chewy509) brought to you by Google Groups!

---------------8<---------------------

;; B0 EXECUTABLE
;; PE FORMAT


;Register renaming

r0 equ rax
r0d equ eax
r0w equ ax
r0b equ al
r1 equ rbx
r1d equ ebx
r1w equ bx
r1b equ bl
r2 equ rcx
r2d equ ecx
r2w equ cx
r2b equ cl
r3 equ rdx
r3d equ edx
r3w equ dx
r3b equ dl
r4 equ rdi
r4d equ edi
r4w equ di
r4b equ dil
r5 equ rsi
r5d equ esi
r5w equ si
r5b equ sil
r6 equ rbp
r6d equ ebp
r6w equ bp
r6b equ bpl
r7 equ rsp
r7d equ esp
r7w equ sp
r7b equ spl


;Default Macros

macro UTF8_STRING name, [string]
{
common
name:
local label,label2
label:
db ((label2-label)/2)-3
db ((label2-label)/2)-3
db string
label2:
db 0
}

format PE64 CONSOLE 4.0
use64

entry start


section '.data' data readable writeable

_B0_strlen_string equ 0
_B0_sys_write__handle equ 0
_B0_sys_write__ptr equ 8
_B0_sys_write__size equ 16
_B0_sys_write_buffer equ 24
_B0_echo_string_handle equ 0
_B0_echo_string_string equ 8

UTF8_STRING B0_DynStr0 ,
048h,065h,06ch,06ch,06fh,020h,077h,06fh,072h,06ch,064h,020h,076h,069h,061h,020h,074h,068h,065h,020h,063h,06fh,06eh,073h,06fh,06ch,065h,0ah,0
UTF8_STRING B0_DynStr1 ,
041h,06eh,064h,020h,061h,06eh,06fh,074h,068h,065h,072h,020h,073h,074h,072h,069h,06eh,067h,0ah,0

db "EXB0 0.0.19",0

_B0__stdin rq 1
_B0__stdout rq 1
_B0__stderr rq 1


align 16
DATA_END:
rb 2000h

section '.code' code readable executable


start:
finit
lea r6,[DATA_END]
call _B0_main

B0_sys_exit:
mov ecx,eax
call [ExitProcess]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; __Init_Win64 Function Code ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

align 8
_B0___Init_Win64:
mov r2, -10
call [GetStdHandle]
mov qword [_B0__stdin], r0
mov r2, -11
call [GetStdHandle]
mov qword [_B0__stdout], r0
mov r2, -12
call [GetStdHandle]
mov qword [_B0__stderr], r0
mov r0, 0
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; strlen Function Code ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

align 8
_B0_strlen:
push r1
push r2
mov r1, qword [r6+_B0_strlen_string]
add r1, 2
xor r0, r0
mov r2, r0
mov r2b, [r1+r0]
test r2, r2
je .B0_END_BLOCK_00004
.B0_END_BLOCK_00003:
add r0, 1
mov r2b, [r1+r0]
test r2, r2
jne .B0_END_BLOCK_00003
.B0_END_BLOCK_00004:
pop r2
pop r1
ret
mov r0, 0
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; End strlen Function Code;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; sys_write Function Code ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

align 8
_B0_sys_write:
mov r2, qword [r6+_B0_sys_write__handle]
mov r0, 0
cmp r2, r0
jne .B0_END_BLOCK_00006
mov r2, qword [_B0__stdin]
jmp .B0_END_BLOCK_00007
.B0_END_BLOCK_00006:
mov r0, 1
cmp r2, r0
jne .B0_END_BLOCK_00008
mov r2, qword [_B0__stdout]
jmp .B0_END_BLOCK_00009
.B0_END_BLOCK_00008:
mov r0, 2
cmp r2, r0
jne .B0_END_BLOCK_000010
mov r2, qword [_B0__stderr]
.B0_END_BLOCK_000010:
.B0_END_BLOCK_00009:
.B0_END_BLOCK_00007:
mov r3, qword [r6+_B0_sys_write__ptr]
add r3, 2
mov r8, qword [r6+_B0_sys_write__size]
lea r9, [r6+_B0_sys_write_buffer]
mov r0, 0
push r0
call [WriteFile]
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; End sys_write Function Code;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; echo_string Function Code ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

align 8
_B0_echo_string:
mov r1, qword [r6+_B0_echo_string_string]
push r0
push r6
add r6, 010h
mov [r6+00h], r1
call _B0_strlen
pop r6
mov r3, r0
pop r0
mov r2, qword [r6+_B0_echo_string_handle]
push r6
add r6, 010h
mov [r6+00h], r2
mov [r6+08h], r1
mov [r6+010h], r3
call _B0_sys_write
pop r6
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; End echo_string Function Code;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; main Function Code ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

align 8
_B0_main:
push r6
add r6, 08h
call _B0___Init_Win64
mov qword [r6+00h], 1
push r0
mov r0, B0_DynStr0
mov qword [r6+08h], r0
pop r0
call _B0_echo_string
mov qword [r6+00h], 1
push r0
mov r0, B0_DynStr1
mov qword [r6+08h], r0
pop r0
call _B0_echo_string
pop r6
mov r0, 0
jmp B0_sys_exit

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; End main Function Code;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


section '.idata' import data readable writeable

dd 0,0,0, RVA KERNEL32_name, RVA KERNEL32_table
dd 0,0,0,0,0

KERNEL32_table:
GetStdHandle dq RVA _GetStdHandle
WriteFile dq RVA _WriteFile
ExitProcess dq RVA _ExitProcess
dq 0

KERNEL32_name db 'KERNEL32.DLL',0

_GetStdHandle dw 0
db 'GetStdHandle',0
_WriteFile dw 0
db 'WriteFile',0
_ExitProcess dw 0
db 'ExitProcess',0




;EOF

.



Relevant Pages

  • example in nasm
    ... WM_INITDIALOG equ 0110h ... push IDC_ARROW ... cmp eax, 0 ... mov ebp, esp ...
    (alt.lang.asm)
  • Re: Why is my nasm program killing itself?
    ... ORIGIN equ 8048000h ... mov bl, al ... mov eax, 1 ... push edx ...
    (alt.lang.asm)
  • Re: 89C51ED2
    ... .EQU EMPTYS,H'2000;"NO ENERGY" will be replaced with new version ... .EQU DEFGMF,H'00;TEAM DEFAULT GAME STYLE ... MOV R3,A;STORE IT READY FOR NEXT TIME INTERRUPT ... JB GFLAG,TIMERC;GAME OVER, NO LEDS ...
    (sci.electronics.design)
  • Re: 89C51ED2
    ... .EQU EMPTYS,H'2000;"NO ENERGY" will be replaced with new version ... MOV R3,A;STORE IT READY FOR NEXT TIME INTERRUPT ... JB GFLAG,TIMERC;GAME OVER, NO LEDS ... LJMP CKLEDS;CLOCK LEDS ROUND ...
    (sci.electronics.design)
  • Re: 89C51ED2
    ... ..EQU EMPTYS,H'2000;"NO ENERGY" will be replaced with new version ... MOV R3,A;STORE IT READY FOR NEXT TIME INTERRUPT ... JB GFLAG,TIMERC;GAME OVER, NO LEDS ... LJMP CKLEDS;CLOCK LEDS ROUND ...
    (sci.electronics.design)