• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Code Puzzle

A couple of additional points: the second assignment of a value to cx is a waste of space, as the register is never used after that point; and it terminates with a display of all zeros rather than all nines.
 
Good lad! :yay: However, it don't work... You've broked it. You've not got the 5 bytes on the front that set the display mode.

I didn't bother with that as it was working for me in a command prompt window; however, it makes sense to add that back in, making 61 bytes.

If you don't mind setting the character attributes as well (7 is the default value) then the whole of the first part (i.e. up to but not including mov di,1e) can be replaced with:

Code:
mov ax,b800
mov ds,ax
mov es, ax
mov ax,0730
xor di,di
mov cx,10
rep stosw

:nerd
 
Thanks Nick, modified as follows...

Code:
A 100
mov ax,2
int 10
mov ax,b800
mov ds,ax
mov ax,30
xor di,di
mov cx,f
ds:
mov [di],al
inc di
dec cx
inc di
cmp cx,0
jne 113
mov di,1E
mov cx,10
mov al,[di]
inc al
cmp al,3a
je 12F
mov [di],al
jmp 11D
mov al,30
mov [di],al
dec di
dec di
cmp di,0
je 13C
jmp 123
mov ax,4c00
int 21

N test.COM
RCX
41
W
Q

This one is 65 bytes and it actually works.
 
If you follow the instructions you will find it obvious that the code hasn't hung.

Code:
;********* File: test.com *************
;
    code     SEGMENT
             ASSUME CS:code, DS:code
             ORG 100h
  
    strt:

	MOV	AX ,0002h
	INT	10				; Video Function Call
	MOV	AX ,B800h
	MOV	DS,AX
	MOV	AX ,0030h
	XOR	DI,DI
	MOV	CX ,0010h
	MOV	DS:[DI],AL
	INC	DI 
	INC	DI 
	DEC	CX 
	CMP	CX,+00h
	JNZ	J00113
J0011D: MOV	DI ,0020h
	MOV	CX ,0010h
J00123: MOV	AL,[DI]
	INC	AL
	CMP	AL,3Ah
	JZ	J0012F
	MOV	[DI],AL
	JMP	J0011D
J0012F: MOV	AL ,30h
	MOV	[DI],AL
	DEC	DI 
	DEC	DI 
	CMP	DI,+00h
	JZ	J0013C
	JMP	J00123
J0013C: MOV	AX ,4C00h
	INT	21				; DOS Function Call

    code     ENDS
             END  strt

->>> JNZ J00113

J00113 label isn't specified in your source code, did you compile it?

I just had to spend an hour trying to locate decent assembler, had to settle on Turbo Assembler :mad
 
This one is 65 bytes and it actually works.

Sadly not: it still has the fail-to-do-the-last-digit problem, by my tests :(

Latest version of mine, which also fails to stop at 99...9, but cut down to do ten digits in order to avoid having to wait for the heat death of the Universe:

Code:
MOV     AX,0002
INT     10
MOV     AX,B800
MOV     DS,AX
MOV     ES,AX
MOV     AX,0730
XOR     DI,DI
MOV     CX,000A
REP STOSW
MOV     DI,0012
MOV     CX,000B
MOV     AL,[DI]
INC     AL
CMP     AL,3A
JZ      0128
MOV     [DI],AL
JMP     0116
MOV     AL,30
MOV     [DI],AL
SUB     DI,+02
JS      0133
JMP     011C
MOV     AX,4C00
INT     21

55 bytes. However, I've just noticed that it still has that redundant assignment to CX :rolleyes:
 
This one is 65 bytes and it actually works.

Code:
inc di
dec cx
inc di
cmp cx,0
jne 113

can be changed to

Code:
inc di
inc di
dec cx
jne 113

saving you three bytes (although as I mention above, you can use rep stosw to avoid having a loop there at all).

EDIT: of course, the two increments of DI can be changed to an ADD, saving you a few more cycles but costing a byte
 
Last edited: 20:30

Advertisers

Back
Top