Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help with Translating a piece of assembler to C

DonToneDonTone Member Posts: 1
Hi all,

As I'm not actually that good in assembler programming, and are writing a piece of software for my company, of which is half is reverse engineering due to programmers/software lost in the obscurity of time, I'd like to ask someone to give me a hand in porting the below piece of (16bit) asm code to C (target platform 32bit--ie windows).

[code]
__asm {
mov BX,16 // set bit counter
RECEIVE:
jz END // stop if counter == 0
shl result,1 // shift receive buffer

mov AX,data
mov DX,port
and AX,0xFC
out DX,AL
mov cx,delayCnt
rcvSts1:
loop rcvSts1
or AX,0x02
out DX,AL // generate 1 clock pulse
mov cx,delayCnt
rcvSts2:
loop rcvSts2
mov DX,recPort
in AX,DX // take bit
mov cx,delayCnt
rcvSts3:
loop rcvSts3
and AL,0x80
jnz ZERO
or result,1 // it is a 1
ZERO: // it is a 0
dec BX
jmp RECEIVE
END: // receive status bit
mov AX,data
mov DX,port
and AX,0xFC
out DX,AL // generate 1 clock pulse
mov cx,delayCnt
rcvSts4:
loop rcvSts4
or AX,0x02
out DX,AL
mov cx,delayCnt
rcvSts5:
loop rcvSts5
in AX,DX // take bit
and AX,80
jz ENDSTAT // status is BAD
mov stat,0 // status is OK
ENDSTAT:
}
[/code]

This is supposed to receive data from a parallel port connected to a m38332 CPU32 BDM interface.
Obviously there is the write equivalent:
[code]
__asm {
mov AX,data
mov DX,port
cmp status,0
jnz NEXT
and AX,0xFE
NEXT:
or AX,0x01
out DX,AL // send status bit to 332
mov cx,delayCnt
sndSts1:
loop sndSts1
and AX,0xFD
out DX,AL
mov cx,delayCnt
sndSts2:
loop sndSts2
or AX,0x02
out DX,AL // generate 1 clock pulse
mov cx,delayCnt
sndSts3:
loop sndSts3
mov BX,16 // init bit counter
SEND:
jz END // stop if bit counter == 0
shl sdata,1 // shift data buffer
jc ONE
and AX,0xFE // if carry, send 1...
jmp ZERO
ONE:
or AX,0x01 // ...else send 0
ZERO:
out DX,AL // send data to LPT
mov cx,delayCnt
sndBit1:
loop sndBit1
and AX,0xFD
out DX,AL
mov cx,delayCnt
sndBit2:
loop sndBit2
or AX,0x02
out DX,AL // generate 1 clock pulse
mov cx,delayCnt
sndBit3:
loop sndBit3
dec BX
jmp SEND
END:
}
[/code]

Now, I know that I'm not supposed to talk to hardware anymore in win32, but that should be solved through a special DLL. For the rest there's the timeing issue, but that I'll somehow take care of, once I know if the problem lies in my assembly translation, or somewhere else.
What I personally came up with (please don't laugh), it the following:
[code]
u16 port = 0x378;
u16 recPort = 0x379;
u16 data = 0x1e;
u16 tmpB = 0x00;
u16 result = 0;
u8 stat = 1;
u16 delayCnt = 2;
int bitcount = 16;

while (bitcount != 0) {
result <<= 1;
tmpB = data;
tmpB &= 0xFC;
__outpb(port, (tmpB & 0x0F));
bdmWait(delayCnt);
tmpB |= 0x02;
__outpb(port, (tmpB & 0x0F));
bdmWait(delayCnt);
tmpB = __inpb(recPort);
bdmWait(delayCnt);
tmpB &= 0x80;
if (tmpB != 0) {
result |= 1;
}
bitcount--;
}
tmpB = data;
tmpB &= 0xFC;
__outpb(port, (tmpB & 0x0F));
bdmWait(delayCnt);
tmpB |= 0x02;
__outpb(port, (tmpB & 0x0F));
tmpB = __inpb(recPort);
bdmWait(delayCnt);
tmpB &= 0x80;
if (tmpB != 0) {
stat = 0;
}
[/code]

and

[code]
u16 data = 0x1e;
u16 port = 0x378;
u16 delayCnt = 2;
int bitcount = 17;
u8 tmpB = 0x00;

tmpB = (u8)data;
tmpB |= 0x01;
__outpb(port, tmpB);
bdmWait(delayCnt);
tmpB &= 0xFD;
outpb(port, tmpB);
bdmWait(delayCnt);
tmpB |= 0x02;
__outpb(port, tmpB);
bdmWait(delayCnt);
while(bitcount != 0) {
sdata = (sdata << 1);
if (!sdata) {
tmpB |= 0x01;
} else {
tmpB &= 0xFD;
}
__outpb(port, tmpB);
bdmWait(delayCnt);
tmpB &= 0xFD;
__outpb(port, tmpB);
bdmWait(delayCnt);
tmpB |= 0x02;
__outpb(port, tmpB);
bdmWait(delayCnt);
bitcount--;
}
[/code]

If somebody could give me hand, you'd really save my day.

Thanks in advance !

/tony
Sign In or Register to comment.