Quite often one needs loops which, in the last iteration, execute only part of the loop body.



Goal

Demonstrate the best way to do this.



Task

Write a loop which writes the comma-separated list

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

using separate output statements for the number
and the comma from within the body of the loop.



Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreach
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges
   Loops/Wrong ranges


    .model small
    .stack 1024
 
    .data   ;no data needed
 
    .code
 
start:
	mov ax,0000h	
	mov cx,0FFFFh	;this value doesn't matter as long as it's greater than decimal 10.
 
repeatPrinting:
	add ax,1 ;it was easier to start at zero and add here than to start at 1 and add after printing.
	aaa	 ;ascii adjust for addition, corrects 0009h+1 from 000Ah to 0100h
	call PrintBCD_IgnoreLeadingZeroes
	cmp ax,0100h		;does AX = BCD 10?
	je exitLoopEarly	;if so, we're done now. Don't finish the loop.
	push ax
		mov dl,","       ;print a comma
		mov ah,02h
		int 21h        
 
		mov dl,20h	;print a blank space
		mov ah,02h
		int 21h
	pop ax
	loop repeatPrinting
exitLoopEarly:
	mov ax,4C00h
	int 21h			;return to DOS
 
PrintBCD_IgnoreLeadingZeroes:
	push ax
		cmp ah,0
		jz skipLeadingZero
			or ah,30h                      ;converts a binary-coded-decimal value to an ASCII numeral
			push dx
			push ax
				mov al,ah
				mov ah,0Eh
				int 10h			;prints AL to screeen
			pop ax
			pop dx
skipLeadingZero:
		or al,30h
		push dx
		push ax
			mov ah,0Eh
			int 10h				;prints AL to screen
		pop ax
		pop dx
	pop ax
	ret
        end start ;EOF