Re: assembly language and reverse engineering



cr88192 wrote:
"Herbert Kleebauer" <klee@xxxxxxxxx> wrote in message


There is no difference in the add, mov, jmp and all the other user

there are differences.

DOS is almost always 16-bit real mode (DPMI and/or manually going into PM
being ignored).

Why? There are exactly the same addressing modes in Real as in Protected
Mode available.

no, they are different.

because, this is a 32-bit memory access, and is thus not applicable to DOS.
we are forced into using 32 bit registers, and not 16 bit ones. this
requires special provisions (for example, because 386-era processors did not
implicitly zero-extend regs, ...).

No wonder this myth will never die if even people who know assembly
programming and who are shown that there is nothing true with this
myth still claim it's true.

A few days ago I posted the source code of small program which finds
the solution for a sudoku puzzle. I will attach it again to this
posting. Please tell me whether this is code for 16 bit real mode DOS
or 32 bit protected mode Windows. And also please tell me, which of
the instructions allowed you to find the answer.


bclr.w #10,sr

;***********************************************************************
; read 81 digits from stdin, anything else is ignored, 0 for unknown
;***********************************************************************

move.l #brett,r5
move.l #81,r2
_10: bsr.l getc
sub.b #'0',r0
blo.b _10
beq.b _20
cmp.b #9,r0
bhi.b _10
eor.l r1,r1
bset.l r0,r1
move.l r1,(r5)
br.b _30
_20: move.l #$03fe0000,(r5)
_30: addq.l #4,r5
dbf.l r2,_10

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

bsr.l display
bsr.l check
bcs.l _err
bsr.l reduce
beq.b _found0

;***********************************************************************
; if not found directly, do one level of recursion of try and error
;***********************************************************************

move.l #brett,r5
move.l #81,r2
_50: move.l (r5),r0
tst.w r0,r0
bne.b _40

lsr.l #16,r0
move.l #2,r1
move.l #9,r4
_60: tst.l r1,r0
beq.b _70
bsr.l save
move.l r1,(r5)
bsr.l reduce
beq.b _found1
bsr.l restore
_70: lsl.l #1,r1
dec.l r4
bne.b _60

_80: lsl.l #16,r0
move.l r0,(r5)

_40: addq.l #4,r5
dbf.l r2,_50
move.l #text4,r0
br.b _130

_found0:move.l #text2,r0
br.b _120

_err: move.l #text1,r0
br.b _130

_found1:move.l #text3,r0
_120: bsr.l display
_130: bsr.l out_text
bsr.l exit

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

save: movem.l r0-r7,-(sp)
move.l #brett,r5
move.l #brett1,r6
br.b rest1

restore:movem.l r0-r7,-(sp)
move.l #brett1,r5
move.l #brett,r6
rest1: move.l #81,r2
rep_r2 move.l (r5)+-,(r6)+-{s1}
movem.l (sp)+,r0-r7
rts.l


;***********************************************************************
; remove all impossible configurations
; carry: 1: unsolveabel
; 0: solveabel
; zero: 1: solved
; 0: not solved
;***********************************************************************

reduce: movem.l r0-r7,-(sp)

_90: move.l #brett,r5
move.l #81,r2
eor.l r6,r6

_50: move.l (r5),r0
tst.w r0,r0
bne.b _40
orq.l #1,r6
lsr.l #16,r0
move.l #2,r1
eor.l r3,r3
move.l #9,r4
_20: tst.l r1,r0
beq.b _10
move.l r1,(r5)
bsr.l check
bcc.b _70
orq.l #-1,r6
eor.l r1,r0
br.b _10
_70: tst.l r3,r3
bne.b _30
move.l r1,r3
br.b _10
_30: orq.l #-1,r3

_10: lsl.l #1,r1
dec.l r4
bne.b _20

tst.l r3,r3
beq.b _err
bmi.b _80
move.l r3,(r5)
br.b _40
_80: lsl.l #16,r0
move.l r0,(r5)

_40: addq.l #4,r5
dbf.l r2,_50

tst.l r6,r6
bmi.l _90
movem.l (sp)+,r0-r7
bclr.w #0,sr ; carry=0 zero=1(solution found) or 0
rts.l

_err: orq.l #1,r0 ; clear zero flag
movem.l (sp)+,r0-r7
bset.w #0,sr
rts.l

;***********************************************************************
; check if legal
; carry: 0: legal
; 1: illegal
;***********************************************************************

check: movem.l r0-r7,-(sp)
move.l #brett,r5
move.l #9,r2
_40: move.l #8,r3
move.l (r5),r0
move.l r0,r1
_30: add.l (r5,r3*4),r0
or.l (r5,r3*4),r1
dec.l r3
bne.b _30

cmp.w r0,r1
bne.l _err
lsr.l #16,r1
or.l r1,r0
cmp.w #$03fe,r0
bne.l _err

addq.l #9*4,r5
dbf.l r2,_40

move.l #brett,r5
move.l #9,r2
_60: move.l #8,r3
move.l (r5),r0
move.l r0,r1
_50: lea.l (r3,r3*2),r4
lea.l (r4,r4*2),r4
add.l (r5,r4*4),r0
or.l (r5,r4*4),r1
dec.l r3
bne.b _50

cmp.w r0,r1
bne.l _err
lsr.l #16,r1
or.l r1,r0
cmp.w #$03fe,r0
bne.l _err

addq.l #4,r5
dbf.l r2,_60


move.l #brett,r5
move.l #3,r2
_20: move.l #3,r3
_10: move.l (r5),r0
move.l r0,r1
add.l 0*36+1*4.b(r5),r0
or.l 0*36+1*4.b(r5),r1
add.l 0*36+2*4.b(r5),r0
or.l 0*36+2*4.b(r5),r1
add.l 1*36+0*4.b(r5),r0
or.l 1*36+0*4.b(r5),r1
add.l 1*36+1*4.b(r5),r0
or.l 1*36+1*4.b(r5),r1
add.l 1*36+2*4.b(r5),r0
or.l 1*36+2*4.b(r5),r1
add.l 2*36+0*4.b(r5),r0
or.l 2*36+0*4.b(r5),r1
add.l 2*36+1*4.b(r5),r0
or.l 2*36+1*4.b(r5),r1
add.l 2*36+2*4.b(r5),r0
or.l 2*36+2*4.b(r5),r1

cmp.w r0,r1
bne.b _err
lsr.l #16,r1
or.l r1,r0
cmp.w #$03fe,r0
bne.b _err

addq.l #3*4,r5
dec.l r3
bne.b _10

addq.l #2*9*4,r5
dbf.l r2,_20

movem.l (sp)+,r0-r7
bclr.w #0,sr
rts.l

_err: movem.l (sp)+,r0-r7
bset.w #0,sr
rts.l

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

display:movem.l r0-r7,-(sp)
move.b #13,r0
bsr.l putc
move.b #10,r0
bsr.l putc
move.l #brett,r5
move.l #9,r3
_40: move.l #9,r4
_30: move.l (r5),r1
addq.l #4,r5
move.l #10,r2
move.b #'0',r0
_20: lsr.l #1,r1
bcs.b _10
inc.l r0
dbf.l r2,_20
move.b #'.',r0
_10: bsr.l putc
dec.l r4
bne.b _30
move.b #13,r0
bsr.l putc
move.b #10,r0
bsr.l putc
dec.l r3
bne.b _40
movem.l (sp)+,r0-r7
rts.l

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

out_text:
movem.l r0-r7,-(sp)
move.l r0,r5
_20: move.b (r5)+-,r0
tst.b r0,r0
beq.b _10
bsr.l putc
br.b _20
_10: movem.l (sp)+,r0-r7
rts.l

;***********************************************************************
;*************** start OS dependent functions **************************
;***********************************************************************
getc:

rts.l

putc:
rts.l

exit:

;***********************************************************************
;***************** end OS dependent functions **************************
;***********************************************************************

text1: dc.b 13,10,"illegal input",0
text2: dc.b 13,10,"directly solved",0
text3: dc.b 13,10,"solved with one recursion",0
text4: dc.b 13,10,"not solveable with one recursion",0

even 4
buf: blk.l 1
brett: blk.l 9*9
brett1: blk.l 9*9
.



Relevant Pages