Thread: Assembler Language Subroutine

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Assembler Language Subroutine

    I'm trying to think of a way to make an OutDecimal subroutine in assembler language, where given a 16 bit integer, the routine prints a string which represents the number. Any idea's on how to start?

    I'm thinking modulus arithmetic and possibly division in order to separate the 16 bit binary number into 4 4bit numbers. Just a thought though...

    Any help would be great, thanks!

    p.s. I'm using a basic simulator with limited instructions, so pseudo code or explanations would help more than code, thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a modulo and divide operation, then something like this:
    Code:
    more:
       load number
       modulo 10
       store result in buffer
       divide number by 10
       if not zero goto more
    printing:
       print last entry of buffer
       if not at start of buffer goto printing
       return
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    [QUOTE=matsp;734741]If you have a modulo and divide operation, then something like this:
    Code:
    more:
       load number
       modulo 10
       store result in buffer
       divide number by 10
       if not zero goto more
    printing:
       print last entry of buffer
       if not at start of buffer goto printing
       return
    The printing part can also be implemented by pushing each digit onto the stack, then, once finished, popping them off and printing them in the order they pop. Saves you from having to make a buffer somewhere.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    I wrote a divide subroutine, i'll have to look into writing one for modulous. That makes sense though, after dividing the values after the decimal points obviously "dissapear". Know anywhere to get good info for the modulo operation? Or a better way to achieve the operation?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the I used push to do a shortest possible print a number for PDP-11. Just make sure you push a marker (such as 0 or -1) to indicate the end of the number).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    nevermind, stupid question. I forgot modulo was just the remainder in a division...So I just modify my routine to record the remainder. Awesome, thanks for the help guys!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by John_L View Post
    I wrote a divide subroutine, i'll have to look into writing one for modulous. That makes sense though, after dividing the values after the decimal points obviously "dissapear". Know anywhere to get good info for the modulo operation? Or a better way to achieve the operation?
    If you do integer division, the modulo should come out as "remainder" - so you should be able to get it "for free" from your division function - when you can't divide any further, the left-over is your "Modulo".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    I got it pretty much working, just not 100% sure how to print the buffer values. Say I have the number 125, my buffer will contain 1, 2, 5 as entries let's say buffer[0], buffer[1], buffer[2]. I can easily load each buffer value into a register, but not sure how to print it. Can I just use the Ascii Table chart and tack on the value 48 and print?
    Last edited by John_L; 03-27-2008 at 05:39 PM.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by John_L View Post
    I got it pretty much working, just not 100% sure how to print the buffer values. Say I have the number 125, my buffer will contain 1, 2, 5 as entries let's say buffer[0], buffer[1], buffer[2]. I can easily load each buffer value into a register, but not sure how to print it. Can I just use the Ascii Table chart and tack on the value 48 and print?
    Exactly

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    nice, it all came together. Couple bugs but other than that it's pretty much complete. Thanks for the help! Probably be back on here later cause I still got a couple of more mini programs to go. Gotta love learning assembler!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembler Language Subroutine 2
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 03-30-2008, 04:48 PM
  2. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 09:09 AM
  3. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. help with assembly language (assembler)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-10-2001, 05:21 PM