Jump to content

How to write this program in assembly!

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Panto

Panto

    Newbie

  • Members
  • Pip
  • 1 posts
hello friends, I have a homework for next week.
I have some troubles in programming and I would be thankful if you can do that.


1) write a program in assembly 8086 language which receive a positive integer number (N),and produce below outputs:

I- show N as a binary number in 16 bit. (Example: if N=334, output is 0000000101001110)

II- show N as a hexadecimal number. (Example: if N=334, output is 14E)

III- sum all digits of N. (Example: if N=334, output is 10)

IV- sum factorielle of all digits. (Example: if N=334, output is 3!+3!+4!=36)


2) write the program in another method

waiting for your reply.

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 402 posts
You have to do some effort on your own. Can you write some trivial assembly?

I am putting the algorithms to do so below. You have to translate them yourself into assembly. If you are stuck some where do let me know.

I - You can write a loop which keeps dividing the number by 2, prints the remainder and makes the quotient the number for next iteration, until the quotient is zero. This will print the binary though in reverse order.

II - You might be able to combine four units (nibble) of binary generated above to produce a single hex digit.

III - In a loop, take the mode of number with 10 and add remainder to sum (sum is initially 0). Then divide the number by 10 and make the quotient as next number.

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
For III, if you have the string version of the number, then you can just iterate through the characters, subtracting 48 ('0') from each, and getting the factorial for each, while adding the factorials in a variable.

I can't do the work for you, but I could include some C code:
int sum_of_digits(char *the_string){ 

char *a= the_string; 

int sum= 0; 

do { 

  if (*a == 0) break; 

  if (*a > 57) break; 

  if (*a < 48) break; 

  

  sum += fac(*a - 48); 

  

  a++; 

} while (true); 

return sum; 

} 
Where fac() is the factorial function.

I think that code should work; I mean, I don't see any reason for it not to.

Edited by RhetoricalRuvim, 27 May 2011 - 09:33 PM.
Forgot the 'return ...'





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users