Click here to Skip to main content
15,891,184 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: It's not the most horrible code but it is pointless Pin
Pete O'Hanlon28-Oct-10 12:31
mvePete O'Hanlon28-Oct-10 12:31 
GeneralRe: It's not the most horrible code but it is pointless Pin
Andy Brummer29-Oct-10 1:38
sitebuilderAndy Brummer29-Oct-10 1:38 
GeneralRe: It's not the most horrible code but it is pointless Pin
fjdiewornncalwe28-Oct-10 13:29
professionalfjdiewornncalwe28-Oct-10 13:29 
GeneralRe: It's not the most horrible code but it is pointless Pin
Al_Brown29-Oct-10 0:49
Al_Brown29-Oct-10 0:49 
GeneralRe: It's not the most horrible code but it is pointless Pin
fjdiewornncalwe29-Oct-10 4:07
professionalfjdiewornncalwe29-Oct-10 4:07 
GeneralRe: It's not the most horrible code but it is pointless Pin
QuiJohn29-Oct-10 4:14
QuiJohn29-Oct-10 4:14 
GeneralRe: It's not the most horrible code but it is pointless Pin
richard_k5-Nov-10 19:31
richard_k5-Nov-10 19:31 
GeneralIt's the small things that matter. Pin
puromtec127-Oct-10 17:24
puromtec127-Oct-10 17:24 
So, yeah, spent a few days (at night, as this is not for my day job) looking for a bug in this code...yep, the bug? Needed to place the return command "ret" after white space, ie. not at the start of a line. Side-effect? It doesn't return, just keeps running the next instruction in memory after ret. WTF | :WTF:

DisplaySelectList:

		
	cls()

	;========================================
	;
	; Input: 
	;	AppSelectListControl.TopPosition
	;	AppSelectListControl.ListItemsIndirect
	;	AppSelectListControl.LineStartAddys
	;		
	;
	; Start reading the LineStartAddys
	;  at the TopPosition position
	;
	; Set the TextInverse if the value in
	;  ListItemsIndirect at position is equal to
	;  SelectedItem value.
	;
	; Determine if TopPosition ptr is the first
	;  line of a list item, if so, then print 
	;  that number.
	; 
	; Print starting at the LineStartAddys[x]
	;  until NULL reached.
	
	ld a, (AppSelectListControl.TopPosition)
	ld (AppSelectListControl.PositionCount), a	; save to variable
	;.breakpoint check_a_for_top_position
	nop
	
LoopPositions:

	;=======================================
	; Check that PositionCount - TopPosition
	;  is not equal to 9, else stop printing
	;=======================================
	
	ld a, (AppSelectListControl.TopPosition)
	ld b, a
	ld a, (AppSelectListControl.PositionCount)
	
	sub b
	
	cp 9
	
	.breakpoint check_z_for_reached_9_a_vs_b
	;==============Base case displayed == 9 ? then stop
	jp z, donePrintingSelectList
		
	
	;=============================================
	; Get the addy of the string and save to stack
	;
	;
	;=============================================
	
	ld de, AppSelectListControl.LineStartAddys	;calculate offset of array start
	
	.breakpoint de_has_LineStartAddy
		
	;calculate offset using TopPosition

	xor a
	ld h, a
	
	ld a, (AppSelectListControl.PositionCount)
	ld l, a
	
	add hl, hl		;double because array is of 2byte items
	
	add hl, de		; offset calculated
	
	;.breakpoint check_hl_for_addy_of_LineStartAddys_of_index
	
	
	;==============
	;-->ld (hl), hl
	;==============	
	ld c, (hl)
	inc hl
	ld b, (hl)
	
	
	push bc		; Save the addy to list item's string
				;  to be used by print string.

	
	;==========================
	; Get next position's addy,
	;  could be 0000
	; 
	;==========================

	inc hl
	ld a, (hl)

	ld (AppSelectListControl.NextAddy), a
	
	inc hl
	ld a, (hl)
	
	ld (AppSelectListControl.NextAddy+1), a
	
	
	;reset hl back to ptr to addy of position
	dec hl
	dec hl
	dec hl
		
	
	.breakpoint just_pushed_bc_as_addy_string

	;;===========================
	;; Read value of addy into hl
	;;===========================
	
	;ld a, (de)
	;ld l, a
	;inc de
	;ld a, (de)
	;ld h, a
			
	;;.breakpoint check_hl_has_value_of_listItem_string
	
	;================================================
	; if addy is null, then stop printing select list
	;================================================


	ld a, b
	or a
	;.breakpoint check_z_for_NULL_listitem_addy1
	jp nz, {+} ; don't quit
	ld a, c
	or a
	;.breakpoint check_z_for_NULL_listitem_addy2
	jp nz, {+} ; don't quit
	
	pop bc

	;.breakpoint check_z_for_1
	jp donePrintingSelectList
+:

	
	
	
	;=====>base case: TopPosition == 0, consider this 
	; line the start so skip to that section
	
	ld a, (AppSelectListControl.PositionCount)
	
	;.breakpoint a_has_position_count
	
	ld b, a
	xor a
	or b
	
	jp nz, NextPosition
	xor a
	
	push af			; push 0 as list item number
	jp printFormattedNumber


; b has current Position (initially was TopPosition)
NextPosition:


	;check the value at PositionCounter - 1 in ListItemsIndirect	
	dec b
	
	;.breakpoint check_b_for_positionCounter_minus_1
	
	ld de, AppSelectListControl.ListItemsIndirect	
	;calc offset using TopPosition
	
	xor a
	ld h, a
	ld l, b	; b has TopPosition - 1
	
	add hl, de
	
	ex de, hl
	
	;.breakpoint check_de_for_addy_of_position_minus_1
	
	; de has the address of the TopPosition - 1 index of
	;  the ListItemsIndirect array
	
	ld a, (de) 	; ld last position's list item number
	
	;.breakpoint check_a_for_lastPositions_listItem_number
	
	push af	; save last list item number
		
	inc de
	ld a, (de)	; de is at Position now
	ld (AppSelectListControl.ListItemPtr), a		; save for later checking highlight status
	;.breakpoint check_listItemPtr_and_de
	
	ld b, a	; ld Position's list item number
	
	
	;.breakpoint check_b_for_listItem_number
	pop af		; get last position's list item number
	
	cp b		; are they the same?
	
	;.breakpoint check_z_for_are_they_same
	

		
		
	jp z, printString	; jump past code for making formatted number

	push bc	

	
printFormattedNumber:
	;===============================================
	; Print a formatted number based on list item on
	;   the stack. (upper-byte)
	;===============================================	
	
	pop af		; get list item number
	.breakpoint check_a_for_listCount1

	
	inc a
	
	
	;.breakpoint check_a_for_listCount2
	
	;.breakpoint a_has_listItem_number
	
	call Convert.AToDecimalText		; hl will point to decimal text w/ null
	
	;.breakpoint check_hl_addy_has_formatted_number
	;======================
	; Print each character
	;======================
	res textInverse, (iy + textFlags)		; do not print highlight
-:

	ld a, (hl)	
	; Check for NULL
	xor b
	cp b
	
	jp z, {+}
	ld a, (hl)
	bcall(_VPutMap)
	
	inc hl	
	jp {-}
+:

	;.breakpoint printed_formatted_number
	
	ld a, '.'
	
	bcall(_VPutMap)
		

	ld a, ' '
	bcall(_VPutMap)
	
	ld a, ' '
	bcall(_VPutMap)

	
	ld a, ' '
	bcall(_VPutMap)
	
	
	
printString:


	;==============================
	; set the text inverse
	;  if listCount == selectedItem
	;==============================
	
	ld a, (AppSelectListControl.ListItemPtr)
	ld b, a
	ld a, (AppSelectListControl.SelectedItem)
	;.breakpoint check_a_for_listcount_and_b_for_selectedItem
	cp b
	
	jp z, SetHighlight
	
	res textInverse, (iy + textFlags)
	jp doneHighlight


setHighlight:
	nop
	;.breakpoint Setting_highlight
	set textInverse, (iy + textFlags)
	
doneHighlight:

	;=================================
	; Print the string starting at the 
	;  addy found on the stack
	;=================================

	pop hl
	
	;.breakpoint printString_start_hl_has_addy
	nop

	;========================================
	;Get the next line break to know when to 
	; stop printing the string.
	;========================================
	
	

-:


	ld a, (hl)
	
	xor b
	
	cp b
	
	jp z, {+}	; quit if null reached

	;========
	; quit if hl matches addy of next line break


	ld a, (AppSelectListControl.NextAddy)
	ld e, a
	ld a, (AppSelectListControl.NextAddy+1)
	ld d, a
	;.breakpoint check_de_for_next_addy
	ld a, l
	cp e
	jp nz, {++}
	
	ld a, h
	cp d
	jp nz, {++}	
	
	
	jp {+}
++:
	ld a, (hl)
	;.breakpoint check_hl_as_chr_ptr
	bcall(_VPutMap)

	inc hl

	jp {-}	
	
+:

	;.breakpoint done_printing_this_line	
	;==================
	; NewLine operation
	;==================
	
	ld a, (penrow)
	ld b, 7
	add a, b
	ld (penrow), a
	xor a
	ld (pencol), a
	
	
	
	;========================
	; Increment PositionCount
	;  then check if addy  
	;========================
	
	inc_byte(AppSelectListControl.PositionCount)
	
	;.breakpoint moved_to_next_position
	
	jp LoopPositions
	
	
donePrintingSelectList:

	.breakpoint done_printing_select_list

	nop

ret         < ------- NEEDS SPACE IN-FRONT OF IT TO WORK...WTF

GeneralRe: It's the small things that matter. Pin
Stephen Hewitt27-Oct-10 19:07
Stephen Hewitt27-Oct-10 19:07 
GeneralRe: It's the small things that matter. Pin
OriginalGriff27-Oct-10 21:41
mveOriginalGriff27-Oct-10 21:41 
GeneralRe: It's the small things that matter. Pin
OriginalGriff27-Oct-10 21:44
mveOriginalGriff27-Oct-10 21:44 
GeneralRe: It's the small things that matter. Pin
puromtec128-Oct-10 4:16
puromtec128-Oct-10 4:16 
GeneralRe: It's the small things that matter. Pin
BillW3328-Oct-10 6:26
professionalBillW3328-Oct-10 6:26 
GeneralRe: It's the small things that matter. Pin
Al_Brown29-Oct-10 0:56
Al_Brown29-Oct-10 0:56 
GeneralRe: It's the small things that matter. Pin
puromtec131-Oct-10 17:16
puromtec131-Oct-10 17:16 
GeneralRe: It's the small things that matter. Pin
fjdiewornncalwe29-Oct-10 4:08
professionalfjdiewornncalwe29-Oct-10 4:08 
GeneralIt's not the most obvious piece of logic. PinPopular
Pete O'Hanlon22-Oct-10 11:51
mvePete O'Hanlon22-Oct-10 11:51 
GeneralRe: It's not the most obvious piece of logic. Pin
AspDotNetDev22-Oct-10 12:10
protectorAspDotNetDev22-Oct-10 12:10 
GeneralRe: It's not the most obvious piece of logic. Pin
Pete O'Hanlon23-Oct-10 0:50
mvePete O'Hanlon23-Oct-10 0:50 
GeneralRe: It's not the most obvious piece of logic. Pin
Peter_in_278022-Oct-10 12:20
professionalPeter_in_278022-Oct-10 12:20 
GeneralRe: It's not the most obvious piece of logic. Pin
PIEBALDconsult22-Oct-10 16:52
mvePIEBALDconsult22-Oct-10 16:52 
GeneralRe: It's not the most obvious piece of logic. Pin
harold aptroot23-Oct-10 11:04
harold aptroot23-Oct-10 11:04 
GeneralRe: It's not the most obvious piece of logic. Pin
Xiangyang Liu 刘向阳23-Oct-10 5:42
Xiangyang Liu 刘向阳23-Oct-10 5:42 
GeneralRe: It's not the most obvious piece of logic. Pin
Pete O'Hanlon23-Oct-10 10:09
mvePete O'Hanlon23-Oct-10 10:09 
GeneralRe: It's not the most obvious piece of logic. Pin
fjdiewornncalwe24-Oct-10 6:47
professionalfjdiewornncalwe24-Oct-10 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.