[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

write z80 program to find largest number in a table

Posted on 2009-02-15
Medium Priority
1,021 Views
hello experts,

I need to write a z80 program to find largest number from a table of integers. I need to store the table of numbers in location 0080h and the largest number in 00E0h. I managed to store the numbers, however I end up putting the final number in location 00E0h. I need help on how to store the largest number instead..Btw, I'm using Z80 Simulator IDE v9.30.

Thank Q experts!
``````;a program to demonstrate use of DB to initialise series of
;numbers and then accept each number one after another.

LD HL, TABLE		;STORE NUMBERS IN HL
LD B,(HL)		        ;PUT NO. OF COUNTER I.E. 6 INTO B
INC HL			;MOVE POINTER TO THE NEXT LOCATION
AGAIN:	LD A,(HL)		;STORE TABLE OF NUMBERS IN A
DEC B			;MINUS EVERY COUNTER BY 1
INC HL			;MOVE TO THE NEXT NUMBER IN TABLE
JP NZ,AGAIN		;JUMP TO 'AGAIN' IF B IS NOT EQUAL TO 0.
;I assume this is where the code to 'compare' the numbers
CALL DISP
HALT

DISP:        	PUSH AF			;this part only display d final number in the table.
PUSH BC
LD (00E0H),A
POP BC
POP AF
RET

ORG 0080H		;LOCATION WHERE THE NUMBER WILL BE STORED
TABLE:	DB 7,3,8,25,6,10,13,4
``````
0
Question by:Timmy904
• 2

LVL 63

Expert Comment

ID: 23648258
The code seems to do what it says - what doesn't seem to be happening is storing the largest number as the routine loops.
perhaps you need to store your first number, then check if the next number is bigger, if not then get the next number, if the next number is bigger than the stored number then store that instead etc..
Currently you seem to store each number in turn so when you finish the routine whaterver the last number is returned.

If this is part of an assignment experts here can't do your homework for you but can help point you in the right direction.
0

Author Comment

ID: 23649552
Hello..

thank u 4 your reply. Yes I can see that I am storing them in sequence but if there's any expert who can show me a way or a code in z80 for getting the largest number in a table I would really appreciate it.
0

LVL 63

Accepted Solution

☠ MASQ ☠ earned 2000 total points
ID: 23650360
If you are using a loop you only need to store one number
Programatically you need to code:

Put zero in the put value location
Start Loop
Grab number from table value X
Bigger than number in put value? Put X in put value location
Else we are going back in the loop
End loop
Push the number in put value location to display the final result

Can you code the insertion of "0" before the loop and then the comparison?
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.