Pergunta

I have to write a microprocessor 8085 assembly language code for the following:-

Population censuses of 6 countries are stored in 18 locations starting from 2400H/0C20H. The first byte is the code number(which can be from 00H to 05H) and the next 2 bytes are the population of the country in binary (so that each country occupies 3 locations). I have to find the country which has the maximum population, and store it's country code number in location 2500H/0C90H.

I have searched all over the internet but couldn't find how do we compare two 2-byte binary numbers in 8085 ? Would be very thankful if you could help.

Foi útil?

Solução

On a 8-bit CPU you compare the first (= highest) byte. If it is equal you compare the second (= lower) byte. When comparing 32- or 64-bit numbers you continue with the third byte and so on. You stop comparison when the bytes are not equal.

On the Z80 you could also use the instruction "SBC HL,DE" to do a 16-bit comparison. However this instruction is not present on 8085.

An example for 8-bit processors:

 LOAD register1, [high byte of A]
 LOAD register2, [high byte of B]
 COMPARE register1, register2
 JUMP_IF_EQUAL was_equal
   // -- This is used for 32- and 64-bit numbers only --
 LOAD register1, [second byte of A]
 LOAD register2, [second byte of B]
 COMPARE register1, register2
 JUMP_IF_EQUAL was_equal
   ...
   // -- --
 LOAD register1, [low byte of A]
 LOAD register2, [low byte of B]
 COMPARE register1, register2
was_equal:
  // Here the status flags are set as if a 16-bit UNSIGNED
  // COMPARE had been done.

Replace the instructions "LOAD" etc. by the correct instructions of your processor!

A signed number comparison is more difficult!

Outras dicas

Solved Finally with the help of a friend. Here it is :-

KickOff 2050H                  ;where to start the program
Org 2050H                      ;where to start the code

lxi sp,23ffH                   ;initializing stack pointer to 23ffH
lxi hl,0000H                   ;resulting population to 00 initially for comparison
shld 2501H                     ;store this poulation (00) to 2501h
lxi hl,2400h                   ;now load the address of start loc of data

mvi c,06h                      ;counter for 6 countries

loop:
     mov d,m                   ;store the index of current country in d register
     inx hl                    ;increase hl pair
     mov b,m                   ;store the first byte of current population in b
     inx hl   
     mov e,m                   ;store the second byte of current population in e
     push hl                   ;push hl pair onto stack for later use
     lxi hl,2501h               
     mov a,b

     cmp m                     ;compare first byte (a-m) will set c=1 if m>a
     jc next                   ;if carry do nothing
     jz next1                  ;if z=1, they are equal so we compare next two bytes
     mov m,a                   ;if current greater than previous value then change values of 2501 and 2502 and also index
     inx hl                     
     mov m,e
     mov a,d
     sta 2500h                 ;ab kitna samjaye, kr lo khud se
     jmp next

next1:
     inx hl
     mov a,e
     cmp m
     jc next
     mov m,e
     dcx hl
     mov m,b
     mov a,d
     sta 2500h

next:
     pop hl
     inx hl
     dcr c
     jnz loop
     hlt

thank you all for your answers !! :)

mvi a, 1st no.  
mvi b, 2nd no.  
cmp b  
jc go   
mov a,b  
go: sta 0020h  
hlt
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top