Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,336 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,006 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



68K Assembly Language help

 
Reply to this topicStart new topic

68K Assembly Language help, adding decimal integers.

giddyupgirl
post 16 May, 2008 - 03:44 AM
Post #1


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 13



Thanked 3 times
My Contributions


Hi wonderful people,

I am a student from lovely sunny Queensland in Australia. I am currently studying for a Bachelor of Technology (Infomation Systems). I am the 1/2 point and am really enjoying the challenge.

This current assignment that I am doing is a little too challenging for me. It has a list of 4 requirements that progressively build on each other. I have managed to get requirement 2 working. I am not sure if it is acceptable to post the assignment specs here or not so I will paraphrase them.

requirement 1: add two digits that are entered seperated by a space and then display the answer ie 6 8 The sum is 14. That is all good, got that working.

requirement 2: modify to accept multi-digit integers with the same number of digits up to 3 almost got that bit under control, it works until there is an overflow. ie the sum results in a 4 digit answer I end up getting an odd hex answer, I think I know where the problem is but really have no idea how to solve it, any help would be greatly appreciated. Once I have that bit sorted then maybe I can move on, or ask for more help rolleyes.gif

requirement 3: modify the program to accept multi-digit integers with numbers of different lengths. This I think I can work out but any hints wouldn't be rejected wink2.gif

and finally requirement 4: modify the program to accept floating point number of up to 3 digits each with exactly the same format,that is , with the same number of digits to either side of the decimal. This I have absolutely no idea how to do, again any hints would be greatly appreciated.

This is the segment of code where I think I have the problem, if more code is need I am happy to add all of it, but thought just for starter this would be a good place to be.

Sorry I am not very good at this, my whole assignment is a mismatch of bits of code found here and there, I am sure there is quite a bit of it that does absolutely nothing. I really do apologise for my ignorance, but I am giving this assignment 120% effort, I am just not getting it very well.

Thanks in advance for any help given, it will be very much appreciated.

CODE
CONVERT        MOVE.W    D2,D0;get the number
            ASR.W    D1,D0;shift the number first right bit
            AND.W    #$000F,D0;discard the other bits for this exercise
                    ;(we can only work on one digit at a time)
            ADD.W    #'0',D0;convert to ascii by adding zero
            CMP.W    #'9',D0;  check if the number is greater than 9 (hex digit max)
            
            BLE        NEXT_BIT;if it is greater than nine move the bit left
            ADD.W    #$07,D0; add seven to the value to take to the next bit
            
NEXT_BIT    TRAP    #14;complete the outch function and display character
            SUBQ.W    #04,D1; subtract 4 to point to the next digit
            BGE        CONVERT;repeat convert subroutine if there are more digits to process
            MOVE.W    D2,D0        
            
            MOVE.W     (SP)+,D1;restore stack registers
            MOVE.W    (SP)+,D2
            MOVE.W    (SP)+,D7
            
            RTS        ;;return from subroutine    


This post has been edited by giddyupgirl: 16 May, 2008 - 05:56 PM
User is offlineProfile CardPM

Go to the top of the page


Anthos
post 17 May, 2008 - 07:46 AM
Post #2


New D.I.C Head

*
Joined: 17 May, 2008
Posts: 1

Hi

Can you please post all of the code you have and I can have a look at it.

thanks
User is offlineProfile CardPM

Go to the top of the page

giddyupgirl
post 18 May, 2008 - 03:13 PM
Post #3


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 13



Thanked 3 times
My Contributions


QUOTE(Anthos @ 17 May, 2008 - 07:46 AM) *

Hi

Can you please post all of the code you have and I can have a look at it.

thanks


Hi,

I have solved one of my problems, and I think I have requirement 3 working, but requirement 4, oh boy not a clue. Can't even find vague examples to pull apart on that one.

CODE


*** DEFINE CfONSTANTS ***
FIRST        DC.W    $2000; memory for first number
SECOND        DC.W    $2100; memory for second number
SUM            DC.W    $2300 ; FOR FINAL SUM OF THE TWO NUMBERS


; EQUATES FOR KERNEL ROUTINES

INCH        EQU     247 ;INCH, READS A KEYBOARD ASCII KEY INTO D0 (PP91)
OUTCH       EQU     248    ;OUT, OUTPUTS ASCII IN D0 TO THE SCREEN (PP 91)
OUTPUT      EQU     243    ;
EXIT        EQU        228    ; EXIT, EXITS BACK TO THE SIMULATOR
LF            EQU        $0A    ; LINE FEED
CR            EQU        $0D    ; CARRIAGE RETURN
NULL        EQU        00    ; NULL VALUE
SPACE        EQU        $20    ; SPACE IN ASCII


LINE_BREAK    DC.B         CR,LF,NULL
PROMPT        DC.B        'Enter two digits separated by a space.',CR,LF,NULL
FINAL        DC.B        'The sum is ',CR,LF,NULL

ADDING        DC.B        $20,NULL    ; to identify the end of the first number (a space)
EQUALS        DC.B        $0D,NULL    ; to identify the end of the calculation (a carriage return)

    ORG    $1000
    
            MOVE.W #$7FFE,SP        ; INITIALISE THE STACK
            
            CLR.L    D0                ; clear d0 for input numbers (for debugging)
            CLR.L    D1                ; CLEAR D1 FOR THE SUM
            CLR.L    D2                ; clear D2 for second number
            
START        MOVE.W    #PROMPT,A1        ; send first message out
            BSR        WRITE_STRING     ; go to write_string to see how

            
            MOVE.W    #ADDING,A1        ; find end of first number
            BSR        GET_NUMBER        ; get first number

            MOVE    D1,FIRST    

            MOVE.B    #OUTCH,D7        ; output space that has been entered
            TRAP    #14                ; tell system to do it
            
            MOVE.L    #EQUALS,A1        ; point to the end of number two
            BSR        GET_NUMBER        ;
            
            MOVE    D1,SECOND        ;store the new number in second memory location
            
            MOVE.L    #LINE_BREAK,A1    ; put a line break in
            BSR        WRITE_STRING     ; write to screen

            MOVE.W  FIRST,D1    
            MOVE.W  SECOND,A2
            ADDA.W  D1,A2            ;do the addition,result in A2
            
            MOVE.L    #FINAL,A1        ; call final message
            JSR        WRITE_STRING     ; write to screen
            
PRINT_DECIMAL                            ;assumes the addition result is in A2
            MOVE.L  #0,D3                ; prepare the print loop counter
            CLR        D4
            MOVE.L  A2,D4                ; copy sum to D4
            BRA        PRINT_DECIMAL_LOOPBODY;jump straight into the decimalisation part of the loop, bypassing the loop test
            
PRINT_DECIMAL_DIV10LOOP
            CMP.W    #0,D4                ;check if the last base 10 division returned 10
            BEQ        PRINT_DECIMAL_PRINTLOOP;if so we can start printing the 0...9 values on the stack
            
PRINT_DECIMAL_LOOPBODY
            DIVU    #10,D4                ;divide by ten to get the 0...9 value in the remainder for
            SWAP    D4                    ;the current least significant decimal value in D4
            MOVE.W  D4,-(sp)            ;push that remainder onto the stack
            MOVE.W     #0,D4                ;clear the remainder
            SWAP     D4                    ;keep the reduced exponent value
            ADD.L    #1,D3                ;increment digit counter
            BRA        PRINT_DECIMAL_DIV10LOOP;jump to loop condition test
            
PRINT_DECIMAL_PRINTLOOP                    ;pops all the pushed base 10 denominators off the stack in a loop
            CMP.L     #0,D3                ;and prints them to the console as it goes so that the most significat
            BEQ        FINISHING            ;quit if we've printed all digits
            
            SUB.L     #1,D3                ;decrement the digit counter index
            MOVE.W  (sp)+,D0            ;pop the next less significant bit off the stack
            ADD.B     #'0',D0                ;make it an ascii numeral
            MOVE.B  #OUTCH,D7                
            TRAP     #14                    ;print it
            BRA        PRINT_DECIMAL_PRINTLOOP;check for next digit
            
***  finishing routine to exit

FINISHING    MOVE.B    #EXIT,D7;call the exit function
            TRAP    #14        ; tell system to do it            

            RTS
            
*** WORKING WITH THE NUMBERS ***            
                
GET_NUMBER    move.w    d7,-(sp) ; pop off stack
            MOVE.W    D3,-(SP);
            MOVE.W    D2,-(SP);
            
            CLR.L    D1        ; clear contents of D1 for new number
            
            MOVE.B    #$03,D3    ; SET MAXIMUM OF D3 TO 3 DIGITS (this will be our counter of how many digits in a number)
            
READ_NUMBER    MOVE.B     #INCH,D7; READ CHARACTER FROM KEYBOARD
            TRAP    #14        ; TELL SYSTEM TO DO IT
            MOVE.L    A1,A0    ; SET ADDRESS POINTER TO THE END OF FIRST NUMBER
        
CHECK_ENTRY                    ; IS THIS THE END OF THE NUMBER
            MOVE.B    (A0)+,D2;get the space or carriage return
            CMP.B    #NULL,D2; is this the last ?
            BEQ        NUMBER_ENTRY; IF IT IS go to the number entry
            CMP.B    D0,D2    ; is it an equals (aka carriage return)
            BEQ        LAST    ; IF IT IS go to the last routin
            BRA        CHECK_ENTRY;ELSE  GO THROUGH THIS ROUTINE AGAIN
            
NUMBER_ENTRY
            CMP.B    #$00,D3    ;SEE THAT THE LIMIT OF DIGITS ISN'T EXCEEDED
            BEQ        READ_NUMBER;IF NOT GET NEXT KEY ENTRY
            
            SUBQ.B    #$01,D3    ;REDUCE D3 COUNTER BY 1
            MOVE.B    D0,D2    ; STORE THE VALUE ENTERED IN D2
            
            MOVE.B    #OUTCH,D7;ECHO KEY ENTERED
            TRAP    #14        ; TELL SYSTEM TO DO IT
            
            MOVE.B    D2,D0    ;COPY ENTERED VALUE BACK TO D2
            SUB.B    #'0',D0    ; convert to a number by subtracting ascii zero value
            CMP.B    #$09,D0    
            BLE        OKAY
            SUBQ.B    #$07,D0    ; IF VALUE OVER 9 THEN TAKE AWAY 7 TO ENSURE IT ISN'T RECORDED AS HEX (aka 10-16 is A-F)
            
OKAY        MULU    #10,D1  ; multiply by 10
            ADD.W    D0,D1        
            BRA        READ_NUMBER;GET NEXT ENTRY
            
LAST        MOVE.W    (SP)+,D2
            MOVE.W    (SP)+,D3
            MOVE.W    (SP)+,D7
            
            RTS
            
*** Other routines, taken from SAMPLES.ASM provided at the start of this subject ***

; ============================================================================

WRITE_STRING     
            MOVE.W D7,-(SP)    ;store modified registers ensuring that the
            MOVE.W D0,-(SP)    ; subroutine is non-destructive
            MOVE.B #OUTCH,D7   ;ready screen output trap
NEXT          MOVE.B (A1)+,D0    ;get the next character in the string
            CMP.B  #NULL,D0    ;exit if null-an explicit check allows the
            BEQ    ENDSTRING; null constant to be defined as non-zero.
            TRAP   #14         ; tell system to do it
            BRA    NEXT           ;get the next character
ENDSTRING      MOVE.W (SP)+,D0    ;restore modified registers ensuring that the
            MOVE.W (SP)+,D7    ; subroutine is non-destructive
            RTS                ;return to the calling procedure
                
            END
            
User is offlineProfile CardPM

Go to the top of the page

mikeblas
post 22 May, 2008 - 06:51 PM
Post #4


D.I.C Head

**
Joined: 8 Feb, 2008
Posts: 153



Thanked 1 times
My Contributions


Hi, Giddyup.

I think solving Requirement #4 is pretty easy, given how far you've already gotten. A little insight into the problem that might help is that adding two numbers with decimals is just as easy as adding two integers without decimals.

CODE

   123456
  +444333
   ------
   567789


just as


CODE

   123.456
  +444.333
   -------
   567.789


If you know that you've always got three digits on either side of the decimal point, you're in great shape; just ignore the decimal when reading input, and output it after the third digit when writing your answer.

If you don't know how many digits here are, it's not so hard. You just have to make a note of where you find the point, flood everything else with zeros, and keep the values aligned around their decimal points.

Everything is easier said than done in assembler, so let me know if you need help coming up with an implementation that reaches that goal.

This post has been edited by mikeblas: 22 May, 2008 - 06:58 PM
User is offlineProfile CardPM

Go to the top of the page

giddyupgirl
post 23 May, 2008 - 08:40 AM
Post #5


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 13



Thanked 3 times
My Contributions


Hi,

Thanks heaps, I have already handed the assignment in, I just couldn't figure out the floating point thing, but what you say makes perfect sense, I just couldn't code it to make it work. I will have to settle for a maximum of 30/40, but for curiosity sake and my own sanity, I would still like to know how to make it work. My friends tell me to let it go, but I can't, I really want to know the answer.

Thanks again for your help
giddyupgirl wub.gif
User is offlineProfile CardPM

Go to the top of the page

Joolia
post 9 Jun, 2008 - 01:32 AM
Post #6


New D.I.C Head

*
Joined: 9 Jun, 2008
Posts: 4

CODE
            
OKAY        MULU    #10,D1 ; multiply by 10
            ADD.W    D0,D1        
            BRA        READ_NUMBER;GET NEXT ENTRY


I would like to enquire re: this part of the code. Why do you have to multiply by 10?
User is offlineProfile CardPM

Go to the top of the page

giddyupgirl
post 9 Jun, 2008 - 08:19 PM
Post #7


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 13



Thanked 3 times
My Contributions


QUOTE(Joolia @ 9 Jun, 2008 - 01:32 AM) *

CODE
            
OKAY        MULU    #10,D1; multiply by 10
            ADD.W    D0,D1        
            BRA        READ_NUMBER;GET NEXT ENTRY


I would like to enquire re: this part of the code. Why do you have to multiply by 10?


Well at the time of posting that it was the only way I could get my output to display as a decimal digit, without that it would display as a hex digit. I am sure that isn't the way to do it, but not having enough knowledge of assembly language this appeared to work for me. I haven't got the results back from that assignment yet so I really can't tell you if it was acceptable or not, but my theory is that the assignment specs didn't say how we were to achieve the desired outputs, just that we had to achieve them, and I did biggrin.gif

cheers
giddyupgirl wub.gif
User is offlineProfile CardPM

Go to the top of the page

Joolia
post 9 Jun, 2008 - 09:22 PM
Post #8


New D.I.C Head

*
Joined: 9 Jun, 2008
Posts: 4

Thanks for explaining! biggrin.gif It sure is an interesting program to ponder on! I tried to make it reach requirement 4 but have either address error or bus trap error... sad.gif But I'll still be trying on it during my free time just for fun. biggrin.gif

This post has been edited by Joolia: 9 Jun, 2008 - 10:07 PM
User is offlineProfile CardPM

Go to the top of the page

Joolia
post 9 Jun, 2008 - 09:57 PM
Post #9


New D.I.C Head

*
Joined: 9 Jun, 2008
Posts: 4

CODE
PRINT_DECIMAL_LOOPBODY
            DIVU    #10,D4            ;divide by ten to get the 0...9 value in the remainder for
            SWAP    D4                ;the current least significant decimal value in D4
            MOVE.W  D4,-(sp)        ;push that remainder onto the stack
            MOVE.W     #0,D4            ;clear the remainder
            SWAP     D4                ;keep the reduced exponent value
            ADD.L    #1,D3            ;increment digit counter
            BRA        PRINT_DECIMAL_DIV10LOOP;jump to loop condition test


I'm wondering abt this part too. Does it mean:

For e.g.: 111 + 2

This part is doing the (11 + 2 = 13)
13 / 10 to get the remainder 3 and swap the remainder (03) => 30
store it using parameter passing => clear this part of D4 and the rest I'm not really sure... How does this part of coding actually work?
User is offlineProfile CardPM

Go to the top of the page

giddyupgirl
post 10 Jun, 2008 - 02:26 AM
Post #10


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 13



Thanked 3 times
My Contributions


QUOTE(Joolia @ 9 Jun, 2008 - 09:57 PM) *

CODE
PRINT_DECIMAL_LOOPBODY
            DIVU    #10,D4        ;divide by ten to get the 0...9 value in the remainder for
            SWAP    D4            ;the current least significant decimal value in D4
            MOVE.W  D4,-(sp)    ;push that remainder onto the stack
            MOVE.W     #0,D4        ;clear the remainder
            SWAP     D4            ;keep the reduced exponent value
            ADD.L    #1,D3        ;increment digit counter
            BRA        PRINT_DECIMAL_DIV10LOOP;jump to loop condition test


I'm wondering abt this part too. Does it mean:

For e.g.: 111 + 2

This part is doing the (11 + 2 = 13)
13 / 10 to get the remainder 3 and swap the remainder (03) => 30
store it using parameter passing => clear this part of D4 and the rest I'm not really sure... How does this part of coding actually work?


To be honest I really don't know, I was grabbing bits of code from where ever I could find it trying to work out a solution, and when this did as I wanted I just accepted it and moved on. tongue.gif I am definitely not as adventurous as you are, I figure if it is working I am not going to question it! I had my exam for that subject last Wednesday, it wasn't as hard as I thought, luckily a large component of it was maths, ie, adding and subtracting binary, hex and floating point and error correcting with hamming code which I am good at, and I think I nailed the coding part too so I was very happy with that. I might even be looking at a distinction for that subject, now that would be funny.

When you do work it out I would love to know the answer out of curiosity, I am doing eCommerce and Managerial Communication this semester, so not quite as challenging or entertaining as assembly language.

Good luck with it.
cheers
giddyupgirl wub.gif
User is offlineProfile CardPM

Go to the top of the page

Joolia
post 11 Jun, 2008 - 04:40 AM
Post #11


New D.I.C Head

*
Joined: 9 Jun, 2008
Posts: 4

QUOTE(giddyupgirl @ 10 Jun, 2008 - 02:26 AM) *

To be honest I really don't know, I was grabbing bits of code from where ever I could find it trying to work out a solution, and when this did as I wanted I just accepted it and moved on. tongue.gif I am definitely not as adventurous as you are, I figure if it is working I am not going to question it! I had my exam for that subject last Wednesday, it wasn't as hard as I thought, luckily a large component of it was maths, ie, adding and subtracting binary, hex and floating point and error correcting with hamming code which I am good at, and I think I nailed the coding part too so I was very happy with that. I might even be looking at a distinction for that subject, now that would be funny.

When you do work it out I would love to know the answer out of curiosity, I am doing eCommerce and Managerial Communication this semester, so not quite as challenging or entertaining as assembly language.

Good luck with it.
cheers
giddyupgirl wub.gif


It's ok then. smile.gif I'll try to figure it out and continue to ponder on the question. I'll let you know when I've got it figured out? biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 04:13PM