Skip to main content

Posts

Showing posts from July, 2010

Positive Integer as Sum of Consecutive Natural Numbers

Any Positive Integer can be represented in the form of Summation of few consecutive Natural Numbers. e.g., Say number is 27 . then it can be represented as 13 + 14 8 + 9 + 10 2 + 3 + 4 + 5 + 6 + 7 So, the following program will take the positive number and show all combination that can make the number as a Summation. import java.lang.*; import java.io.*; public class NumSum { public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader ( new InputStreamReader(System.in)); System.out.print("\n Enter a Positive Number:: "); int n=Integer.parseInt(in.readLine()); String seq = new String(); if(n>0) { for(int i=(n/2)+1; i>=1; i--) { boolean f=true; int s=n; int j=i; for(; j>1 || s>=0; j--) { s-=j; if(s==0) { f=false; break; } } if(!f) { System.out.println(); for( ; j<i; j++) { System.out.print(j + "

Summation of an Array-full Numbers

This Program will add an Array of 8 bit Numbers with Carry. Assume that the size of Array is stored at the location 2050H and Array starts from 3000H. The Result will be stored from 4000H. Memory Loc Hex Code Label Mnemonics Comment 3050 3051 3052 3A 50 20 START: LDA 2050H ;Take the Size of Array in A ( Accumulator ) 3053 47 MOV B, A ;Store the Array Size in B 3054 3055 3056 21 00 30 LXI H, 3000H ;Initialize the Array pointer (HL Register Pair) with Address of First Element 3057 3058 16 00 MVI D, 00H ;Reset the Carry Holder (D Register) for Addition 3059 305A 3E 00 MVI A, 00H ;Reset the Result Holder (Accumulator) 305B 86 LOOP: ADD M ;Add Accumulator Content with Each Element of Array 305C 305D 305E D2 60 30 JNC NOCARRY ;If Carry is not created don't increase Carry Holder 305F 14 INR D ;If Carry is generated the Carry Holder will be increased by 1 3060 23 NOCARRY: INX H ;Point the next Element of the Array 3061 05 DCR B ;Decrease the Loop Scope

0s and 1s Count in 1 Byte Memory Word

This program will count all the 1s and 0s individualy presents in a 1 byte word. And store the result in individual places in Memory. Suppose, argument byte is stored in memory location 2050H and store the result of 1's count at 2060H memory location and 0's at 2061H Memory Location Hex Code Label Mnemonics Comment 3050 3051 3052 3A 50 20 START: LDA 2050H ;Load Accumulator with the number stored at location 2050H 3053 3054 06 00 MVI B, 00H ;Initiate 0's Counter 3055 3056 0E 00 MVI C, 00H ;Initiate 1's Counter 3057 3058 16 08 MVI D, 08H ;Initiate Bit Counter 3059 07 LOOP: RLC ;Rotate Accumulator Left with Carry 305A 305B 305C DA 61 30 JC ONE ;If 1 is found count 1 305D 04 INR B ;If 1 is not found it must be 0. So increase 0's Counter 305E 305F 3060 D2 62 30 JNC NEXT ;Go to next Bit 3061 0C ONE: INR C ;Increase 1's Counter 3062 15 NEXT: DCR D ;Decrease the Bit Counter 3063 3064 3065 C2

2's Complement of 2Byte Number

This program will work out the 2's Complement of a 2 Byte Number. As Microprocessor can perform 8 bits at a time in Accumulator, so we follow the foloowing Algo Fetch the 2 Byte Number in a Register Pair Bring Least Significant Byte of the Number from last Register into A (Accumulator) Make Complement of A and add 1 Store back the Updated LSByte into its source Register Bring MSByte of the Number from another Register into A Make Complement of A and add 1 if carry was genetrated at the time of LSByte opeartion Store back the Updated MSByte into its source Register Store the Result to a Memory location Suppose, Argument Number is stored in Location 1000H and 1001H Memory Location and store the value in 2000H and 2001H Memory Locations. Memory Location Hex Code Label Mnemonics Comment 3050 3051 3052 2A 00 10 START: LHLD 1000H ;Load the HL Register Pair with the content of Memory Location 1000H and 1001H 3053 7D MOV A, L ;Move the Content of L to A ( Accumulator ) 3054 2F