Simple Addition Issues - Result is a letter

Post programming questions related to the CPC Here!
Post Reply
MCSens
Posts: 3
Joined: Wed May 01, 2019 1:07 pm

Simple Addition Issues - Result is a letter

Post by MCSens » Wed May 01, 2019 6:59 pm

Hi,

I can't seem to get Addition running properly without using Basic as in the Tutorial.
The result always seems to 'overflow'.

My Code:

Code: Select all

PrintChar equ &BB5A ;AMSTRAD Firmware Function prints a
WaitChar equ &BB06 ;AMSTRAD Firmware Function waits for input and sets to a

org &8000
	;First Operand
	Call WaitChar
	Call PrintChar
	ld (&9000), a
	
	;Print +
	ld a,'+'
	Call PrintChar

	;Second Operand
	Call WaitChar
	Call PrintChar
	ld (&9001), a

	;Print =
	ld a,'='
	Call PrintChar

	ld bc,(&9000)
	ld a,c
	add b ;DOES NOT WORK?!?!?!?!
	Call PrintChar
ret
When I log the Values of c and b for instance with

Code: Select all

	
	ld a,b
	Call PrintChar
	ld a,c
	Call PrintChar
they look correct.

My understanding:
I use the Firmware Function to get user input and assign that to 9000 and 9001.
By using bc c should contain the value of 9000 and b should contain the value of 9001.
But when I add these two values, the result always is some letter.

So for instance 5+2=k

What am I doing wrong here?

Indyuk2000
Posts: 10
Joined: Wed Apr 24, 2019 11:59 pm

Re: Simple Addition Issues - Result is a letter

Post by Indyuk2000 » Wed May 01, 2019 7:56 pm

Hi,

I think the problem is with the &BB06 call. According to the firmware register 'a' holds the value of the character, which I think it means by that the ASCII code. Of course that value will not be the literal value your supplying. For example, if you entered 1 into b the value it's going to actually use is 49 because that is the ASCII representation of it.

I haven't done any of this myself but Keith did an excellent tutorial on addition and subtraction in one of his CPC videos.

MCSens
Posts: 3
Joined: Wed May 01, 2019 1:07 pm

Re: Simple Addition Issues - Result is a letter

Post by MCSens » Wed May 01, 2019 9:13 pm

Thanks for the tip, I tried something like that now

Code: Select all

	
	ld bc,(&9000)
	ld a,c
	sub a, 48
	add b ;Works now?
	Call PrintChar
which works.

So I am now just wondering why the Example from Lesson 3 with BASIC works without such a conversion.

(Time flies by SOOO quickly playing around with this thing :D )

Indyuk2000
Posts: 10
Joined: Wed Apr 24, 2019 11:59 pm

Re: Simple Addition Issues - Result is a letter

Post by Indyuk2000 » Wed May 01, 2019 9:42 pm

I think Keith's code is slightly different from what you are trying. He doesn't use the WaitChar call for user input. Instead he "pokes" the values directly to the source memory locations. Then having performed the math operation using assembly, he fetches (using peek) the result directly from resultant memory location and displays it using BASIC. I think for the sake of simplicity he's mixing the best of both worlds to demostrate simple maths.

I think in one of his other videos he covers a section on how to convert values from memory into a format that can be output to the screen. I myself will be looking into this as I need to find out how to generate and display game score.

User avatar
akuyou
Posts: 563
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Simple Addition Issues - Result is a letter

Post by akuyou » Wed May 01, 2019 11:27 pm

Lesson 3 'Cheated' and used basic to read in, and write out the result - Basic coverts Hex (00-FF) to Decimal (0-255) for us in that example...

WaitChar and Printchar are working in ascii as you've observed,

below is a piece of code, quickly nicked from ChibiAliens, which should show a number from 0-255 to the screen in DECIMAL... this should compile in Winape on it's own

Now if you want to read a number >10 you would want to do some kind of reverse operation, reading in 3 letters, and adding them to a register... if you want, have a go, and see if you can make it... I'll make something if you get stuck!

Code: Select all

PrintChar equ &BB5A ;AMSTRAD Firmware Function prints a

	org &8000
	ld a,100
	call ShowDecimal
	call ShowDecimal
	ret

ShowDecimal:
	push af
		call DrawText_Decimal
	pop af
	ret

DrawText_Decimal:	;Draw a 3 digit decimal number (non-BCD)
	ld hl,&640A
	ld b,a		
	
	cp h
	jr nc,DecThreeDigit
	
	call PrintSpace
	cp l 
	jr nc,SkipDigit100
	call PrintSpace
	jr SkipDigit10
	
DecThreeDigit

	call DrawTextDecimalSub
SkipDigit100:
	ld h,l
	call DrawTextDecimalSub
DrawTextForceZeroCall_Plus3:
SkipDigit10:
	ld a,b
DrawText_CharSprite48:
	add 48
DrawText_CharSpriteProtectBC:
	jp PrintChar
PrintSpace:
	push af
		ld a,' '
		call printchar	
	pop af
	ret

DrawTextDecimalSub:
	ld a,b
	ld c,0
DrawText_DecimalSubagain:
	cp h
	jr c,DrawText_DecimalLessThan	;Devide by 100
	inc c
	sub h
	jr DrawText_DecimalSubagain
DrawText_DecimalLessThan:
	ld b,a
	ld a,c
	or a		;We're going to do a compare as soon as we return
	jr DrawText_CharSprite48
	

Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

Post Reply

Return to “Amstrad CPC Assembly Programming”