next up previous
Next:Rex86 Sample - Parsing Rex86 Up: Overview Previous: Rex86 Sample - Char Upper to Lower


Rex86 Sample - Division

;given

ADD
SUB

CMP
COND
JMP

MOV


;assume AX<-A
;assume BX<-B
;output Q in BX
;       R in AX
;start
MOV   CX,0
;loop
CMP   AX,BX
COND  <   done
SUB   AX,BX
ADD   CX,1
JMP   loop
;done
MOV   BX,CX

; how to teach

; first case
;17/7 Q 2 R 3
; probably too hard

; attempt to solve
;3/5 Q 0 R 3
; produces the following

;start
CMP   AX,BX
COND  <   done
;done
MOV   BX,0

; but what if A>=B?
;12/8 Q 1 R 4
; seems like we should A-B

MOV   CX,0
;start
CMP   AX,BX
COND  <   done
SUB   AX,BX
MOV   CX,1
;done
MOV   BX,CX

; and again back to the first case
;17/7 Q 2 R 3
; currently we get Q 1 R 10

; eureka, use a loop

MOV   CX,0
;start
CMP   AX,BX
COND  <   done
SUB   AX,BX
ADD   CX,1
JMP   start
;done
MOV   BX,CX


next up previous
Next:Rex86 Sample - Parsing Rex86 Up: Overview Previous: Rex86 Sample - Char Upper to Lower

by dlong@progmatism.com. Plz don't copy kthx.