I'm about to translate this C-code into Asm.

How can this be translated:


???== I don't know.


case WM_CREATE:
hdc = GetDC (hwnd);
GetTextMetrics (hdc, &tm);
cxChar = tm.tmAveCharWidth; ???

is this the same as:
mov tm.tmAveCharWidth,cxChar
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ; ???
cyChar = tm.tmHeight + tm.tmExternalLeading; ???

ReleaseDC (hwnd,hdc);
return 0;


case WM_PAINT:
hdc = BeginPaint (hwnd,hdc) ;
for (i = 0 ; i < NUMLINES; i++) ???
Posted on 2004-08-31 08:42:55 by The SharK
For first, its the other way around and you can't do memory to memory so try
mov eax,tm.tmAveCharWidth

mov cxChar,eax


And for third
mov eax,tm.tmHeight

add eax,tm.tmExternalLeading
mov cyChar,eax


For last
mov ecx,0

cmp ecx,NUMLINES
jle endLoop
startLoop:

; this is the body of the loop where you can
; do something, ecx is the "i" variable

add ecx,1 ; increment and test ecx
cmp ecx,NUMLINES
jg startLoop
endLoop:
Posted on 2004-08-31 09:08:26 by Eóin
The second is messy, the first bit of it
(tm.tmPitchAndFamily & 1 ? 3 : 2)

performs a check to see if tm.tmPitchAndFamily AND 1 is true or false (non zero or zero) if its ture the check returns 3, if false it returns 2. This can be coded with conditional jumps, conditional moves or some fancy binary operations. I'll give the easier to follow jumps version. Unfortunatly this is also the slower.
mov eax,tm.tmPitchAndFamily

and eax,1
jz falseResult

trueResult:
mov eax,3
jmp endCheck

falseResult:
mov eax,2

endCheck:

finally it performs some calculations with this result, thankfully its not too difficult
mov edx,cxChar	; edx = cxChar

shr edx,1 ; edx divided by 2

;assuming eax is still our result for previous bit
imul eax,edx ; eax = eax * edx
mov cxCaps,eax


I haven't tested this so ask if theres mistakes....
Posted on 2004-08-31 09:21:44 by Eóin
; Switch (uMsg)

mov eax, uMsg
; case WM_XXX:
.if eax == WM_XXX
: : :
; case WM_YYY:
.elseif eax == WM_YYY
: : :
; case WM_CREATE:
.elseif eax == WM_CREATE

; hdc = GetDC (hwnd);
invoke GetDC, hwnd
mov hdc, eax

; GetTextMetrics (hdc, &tm) , & is address of value
invoke GetTextMetrics, hdc, addr tm

; cxChar = tm.tmAveCharWidth; ???
m2m cxChar, tm.tmAveCharWidth ; 'm2m' in masm32 macro.asm

; cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2)
mov eax, tm.tmPitchAndFamily
and eax, 1 ; Mask LSBit, maybe odd/even ?
.if eax
mov eax, 3 ; take 3, if LSBit=1 or odd
.else
mov eax, 2 ; take 2, if LSBit=0 or even
.endif
mov ebx, cxChar ; * cxChar
mul ebx ; eax = eax * ebx
shr eax, 1 ; / 2 ; ???
mov cxCaps, eax

; cyChar = tm.tmHeight + tm.tmExternalLeading; ???
mov eax, tm.tmExternalLeading
add eax, tm.tmHeight
mov cyChar, eax

; ReleaseDC (hwnd,hdc);
invoke ReleaseDC, hwnd, hdc
return 0 ; 'return' in masm32 macro.asm


P.S: MASM(.if/.elseif) like C(Switch/Case), but not same!

routine running in a (case) , when it run to end, will be continue to next Case compare. but (.elseif) not ..

Ex:
;    case    1

; case 2
; case 3
; do something
; .break ; not continue to case 4
;
; case 4

.elseif eax==1 || eax==2 || eax==3
do something

.elseif eax==4
Can you guess my mean?

; hdc = BeginPaint (hwnd,hdc) ;

invoke BeginPaint, hwnd, hdc
mov hdc, eax

; for (i = 0 ; i < NUMLINES; i++) ???
; BASIC, for i = 0 to NUMLINES-1 step 1
mov i, 0
;--- --------------
_nx:

???

inc i ; i++ is i=i+1
.if i < NUMLINES
jmp _nx
.endif
;--- --------------
Posted on 2004-08-31 10:21:26 by Kestrel
The SharK,

you can use the -S (or -Fa) switch of your c compiler to get a assembler output :wink:

ancev
Posted on 2004-08-31 19:05:12 by ancev
"cl /Od /FAs file.c" or "cl /o1 /FAs file.c" are good methods to get asm listing from C code when you're in a hurry :) - if you use max optimizations (/Ox, or a longer hand-crafted list of options), the code isn't as easy to follow though..
Posted on 2004-09-01 05:07:02 by f0dder
guess I have to install VS 6.0 :)
Posted on 2004-09-01 11:41:07 by The SharK
Or VS2003 toolkit which is 100% free and pretty neat.
Posted on 2004-09-01 11:52:59 by f0dder
The second is messy, the first bit of it
(tm.tmPitchAndFamily & 1 ? 3 : 2)

performs a check to see if tm.tmPitchAndFamily AND 1 is true or false (non zero or zero) if its ture the check returns 3, if false it returns 2. This can be coded with conditional jumps, conditional moves or some fancy binary operations. I'll give the easier to follow jumps version. Unfortunatly this is also the slower.
mov eax,tm.tmPitchAndFamily

and eax,1
jz falseResult

trueResult:
mov eax,3
jmp endCheck

falseResult:
mov eax,2

endCheck:


In this particular case you can do this:


; if 0001 then 0011
; if 0000 then 0010
; in both cases bit 0010 gets set

mov eax,tm.tmPitchAndFamily
and eax,1
or eax,2
Posted on 2004-09-02 01:51:27 by Morris