View Full Version : two 64 bit numbers addition
anon10457
05-01-2013, 05:14 PM
Hi,
In slide 8.4 an exercise asks to add two 64 bit numbers and count the no. of carry generated also ?
Please give a little hint cos have no idea how to begin with. ?
All the register I came across till now are of 16 bit only then how will two 64 bit nos will suffice? Moreover if we add one bye (from LSB) or two bytes at a time and how will the selection of proper bytes will take place?
Thanks,
Anon10457
basant
05-01-2013, 07:51 PM
Anon10457,
Obviously the numbers will not fit into registers. Hence you need to keep them in the memory. Therefrom you can load them byte by byte into two separate registers and do the addition in a loop.
-- Basant
anon10457
06-01-2013, 12:51 AM
Hi,
I wrote a code for subroutine:
MOV R0,#8
ACALL: SUBRT
END
SUBRT: CLR A
MOV R0, #4
MOV R1,#1
MOV R2,#4
MOV A,R0
MOV B,R1
RET
Error generates:
Compiling file: 9.1.1
Initializing pre-processor ...
Warning at 2 in 9.1.1: Reserved keyword used as label
Syntax error at 2 in 9.1.1: Unknown keyword: `SUBRT'
`SUBRT' is neither macro nor instruction nor directive
Pre-processing FAILED !
I am not exactly getting ? Everything seems to be fine still the error?
Please guide.
Thanks,
Anon10457
basant
06-01-2013, 08:17 AM
Anon10457,
Please follow the error. It says that "Reserved word is getting used as label". Now you need to find which reserved word you are using as a label. A proper glance at all the labels will reveal the issue.
Also when you use QUOTE, you should end it properly with "/QUOTE". Currently you write "QUOTE\" which is incorrect. Also you should preview the post before sending.
-- Basant
anon10457
06-01-2013, 11:16 AM
Hi,
1.MOV R0,#8
2.CALL SUBRT
3.MOV A,#09
4.SUBRT: CLR A
5.MOV R0, #4
6.MOV R1,#1
7.MOV R2,#4
8.MOV A,R0
9.MOV B,R1
10.RET
11.END
In the code shown above the subroutine is called many a times (I analysed via single stepping) why is this happening? Also the values of register R4 and R6 along with memory location 008H changes to 05 although I am not playing at all with them in the code.
Program execution takes place this way:
step1,step2 then jumps to step4(here it changes value of memory location 008H to 05..i don't understand why this happened? )till step 10. Then to step 3(as it should and so it does) then agains goes into the subroutine. why so?
Should not the subroutine execution takes place just once and as it reaches to step 3 should jump to step 11 to END the code? Jumps back to step1 again after second execution of subroutine. And as it reaches to step 4 from step2 value of R6 changes to 05. Just like in previous case it happened with 008H .
Please guide. I've enclosed the attachment.
Thanks,
Anon10457
basant
06-01-2013, 12:12 PM
Anon10457,
Your program is doing what you are asking it to do. If you want your subroutine to get executed only once, then you should put END right after step 3. Otherwise CPU will try to execute the next instruction which is the beginning of your subroutine. Think subroutine like a jump. The only difference between jump and subroutine call is that return address is getting saved by the CPU and as part of RET instruction, CPU jumps back to the return address.
By the way, what exactly are you trying to achieve through this code?
-- Basant
anon10457
06-01-2013, 12:42 PM
Hi,
I had tried that also i.e putting END just after step3 but displays error
Compiling file: 9.1.1
Initializing pre-processor ...
Syntax error at 2 in 9.1.1: Symbol not defined: subrt
Pre-processing FAILED !
Creating code listing file ... -> "9.1.lst"
1 error, 0 warnings
What could be the probable mistake ?
Through this code I was just estimating how actually does subroutine works and called( with the help of single stepping n all).
Thanks,
Anon10457
basant
06-01-2013, 03:00 PM
Anon10457,
I think that the error is coming because asembler expects that nothing will come after END.
You can also put entire subroutine code before the main code. I think that it should work fine.
-- Basant
anon10457
06-01-2013, 03:09 PM
Hi,
Anon10457,
You can also put entire subroutine code before the main code.
After doing the same it is now just looping around subroutine section and does not even bother about the MAIN section. Total time to execute(29 micro sec).
Code:
SUBRT: CLR A
MOV R0, #4
MOV R1,#1
MOV R2,#4
MOV A,R0
MOV B,R1
RET
MAIN: MOV R0,#8
CALL SUBRT
MOV A,#09
END
Thanks,
Anon10457
basant
06-01-2013, 03:53 PM
Anon10457,
Have you put the following two lines at the very beginning?
ORG 0h
JMP MAIN
-- Basant
anon10457
07-01-2013, 01:41 AM
Hi,
I've tried to write a code to add two 64bit nos but leaving the LSB the other results are not displaying the desired value.
The two nos (64bit) along with the result(as per the calc) is shown below:
1A 2B 3C 4D 5E 6F 7A 8B
+8A 7B 6C 5D 4E 3F 2A 1B
A4A6A8AAACAEA4A6
Code :
ORG 0H
JMP MAIN
SUBRT:
MOV A,@R0
MOV B,@R1
ADDC A,B
MOV 038H,A
JNC L5
L5: INC R7
DEC R0
DEC R1
RET
MAIN: CLR C
MOV R7,#0
MOV 017H,#8BH
MOV 016H,#1BH
MOV 015H,#6FH
MOV 014H,#5EH
MOV 013H,#4DH
MOV 012H,#3CH
MOV 011H,#2BH
MOV 010H,#1AH
MOV 025H,#1BH
MOV 024H,#2AH
MOV 023H,#3FH
MOV 022H,#4EH
MOV 021H,#5DH
MOV 020H,#6CH
MOV 019H,#7BH
MOV 018H,#8AH
MOV R0,#025H
MOV R1,#017H
MOV R2,7
L4: LCALL SUBRT
DJNZ R2,L4
END
I've used memory location 038h to store the resulted value as was shortage of registers who can hold the memory location else had formed a loop which would have automatically print the result to successive memory location.
Where could be probable error that I am not getting the desired output.?
Thanks,
Anon10457
Anon10457,
Did you single-step through your code to see what is going on? Additionally I will recommend that instead of directly attempting 64bit additions, you can attempt 16bit additions first. Once you have that working, you can extend it for 64bit additions.
Thanks,
Anup
anon10457
09-01-2013, 12:08 AM
Hi,
Code for 32bit addition works fine:
ORG 0H
JMP MAIN
SUBRT:
MOV A,@R0
MOV B,@R1
ADDC A,B
MOV 038H,A
JNC L5
L5: INC R7
DEC R0
DEC R1
RET
MAIN: CLR C
MOV 017H,#8BH
MOV 016H,#1BH
MOV 015H,#6FH
MOV 014H,#5EH
MOV 025H,#1BH
MOV 024H,#2AH
MOV 023H,#3FH
MOV 022H,#4EH
MOV R0,#024H
MOV R1,#016H
MOV R2,#3
MOV A,025H
ADD A,017H
L4: LCALL SUBRT
DJNZ R2,L4
END
FOR SAMPLE:
5E 6F 1B 8B
+4E 3F 2A 1B
AC AE 45 A6
But the problem persists with the 64 bit addition i.e 7th and 8th bytes in addition is causing the problem i.e not giving the desired output. I've come to know the reason also of it so I wish to know if there is any concept to add the nos stored in the two different registers of different register bank? ?
Thanks,
Anon10457
anon10457
09-01-2013, 12:36 AM
Hi,
64bit addition is also working fine now.
ORG 0H
JMP MAIN
SUBRT:
MOV A,@R0
MOV B,@R1
ADDC A,B
MOV 038H,A
JNC L5
L5: INC R7
DEC R0
DEC R1
RET
MAIN: CLR C
MOV R7,#0
MOV 017H,#8BH
MOV 016H,#7AH
MOV 015H,#6FH
MOV 014H,#5EH
MOV 013H,#4DH
MOV 012H,#3CH
MOV 011H,#2BH
MOV 010H,#1AH
MOV 027H,#1BH
MOV 026H,#2AH
MOV 025H,#3FH
MOV 024H,#4EH
MOV 023H,#5DH
MOV 022H,#6CH
MOV 021H,#7BH
MOV 020H,#8AH
MOV R0,#026H
MOV R1,#016H
MOV R2,#7
MOV A,027H
ADD A,017H
MOV 038H,A
L4: LCALL SUBRT
DJNZ R2,L4
END
Still would want to know if the addition between two different/same registers of different memory banks is possible or not?
Also, in the code above I wish to store the result(each byte) to different location in memory but the due to the shortage of registers (As R0,R1 utilized) which access/hold the memory address ,could not do so. Please give any hint to do so.
Thanks,
--Anon10457
basant
09-01-2013, 09:47 AM
Anon10457,
You could take the following approach.
Use register bank 0 for registers
Store arguments in locations corresponding to register 2 and 3 or scratch pad RAM
Use ADDC with direct memory location
Store the results back in memory
Since 8051 ISA allows you to perform addition on direct memory location, in principle it is possible to use any memory location including that of any register banks to perform the addition.
-- Basant
anon10457
10-01-2013, 12:20 AM
Hi,
In slide 11.2 its written that clock runs at half the crystal frequency....what does it mean? I am unable to makeout ?
Does it means that 1 machine cycle takes half of XTAL frequency. ?
Or Say crystal is pulsing 100times per second..than 8051 can execute 50 single cycle instruction per second.?
Confused.
Thanks
Anon10457
Anon10457,
Yes, it means that if crystal is oscillating at 100 Hz, then the system clock is 50 HZ.
Thanks,
Anup