0

i am trying to learn assemly lang. This is the first program i am trying to run. the problem i am having is that arr is being loaded and instisd it loads the 1,2 right but then it loads the 12 as a c. then when i add it it addes the letter + the numbers and gives the wrong answer. thank you for any help.

org $10000

     clr.l     d0        ; i = 0, we use the .l since i takes part in .l calculation
     clr.l     d3        ; sum = 0 (temporarily sum lives in d3)
     move.w    n, d1     ; d1 = n

loop cmp.w     d1, d0
     bge       endloop   ; if (i >= n) goto endloop

     movea.l   #arr, a0  ; a0 = &arr[0]
     adda.l    d0, a0   
     adda.l    d0, a0    ; a0 = &arr[0] + 2 x i
     add.w     (a0), d3  ; d3 = d3 + arr[i]

     addq.w    #1, d0    ; i = i + 1
     bra       loop      ; repeat loop

endloop
     move.w    d3, sum   ; write the sum into memory


org $11000

arr        dc.w     1, 2, 12, 4, 21
n          dc.w     5
sum      dc.w     0

P.S i do under stand that the decimal 12 is Hexadecimal C i really need to know how to use the decimal number not the hex

user2615302
  • 194
  • 2
  • 14
  • What assembler are you using? – fvu Nov 06 '13 at 15:02
  • 1
    What do you mean by "gives the wrong answer"? Decimal and hexadecimal are textual representations and have no relevance when performing arithmetic on numeric values. – Michael Nov 06 '13 at 15:06
  • it is wrong becouse it is adding the hexadecimal and i need it to add the decimal – user2615302 Nov 06 '13 at 15:09
  • There's no difference. The numbers are exactly the same. The base only matters when you view the numbers in text form. – Michael Nov 06 '13 at 15:12
  • 0x0C (C in hex) is exactly the same as 12 decimal. It's not a "letter"; it's a hex digit (number). Adding `1+2+12` and `1+2+C` are exactly the same thing, and they both add up to `15`. – Ken White Nov 06 '13 at 15:17
  • 4
    _"ok then why when i run this i get 1+2+12=5"_. If I run this with `n` set to 3 (to add 1, 2 and 12) I get `D3` = 0x0000000F, which is 15, which is the correct result. – Michael Nov 06 '13 at 15:19
  • ok, maybe i have been looking at this wrong thanks for your help – user2615302 Nov 06 '13 at 15:22

1 Answers1

1

I tested your program in easy68k and it works ok.

When dealing with numbers in assembly language, keep in mind that processors only understand binary arithmetics. Assembler softwares deal with hexadecimal for programmer's ease, but in the end binary arithmetics is the only that matters.

That being said, easy68k will display values in hex instead of binary or decimal because hex is compact, but whenever you see a value in a memory cell, you have to discard its first look and interpret it.

For example: in your case, in D3 there is an unsigned integer number, displayed in hexadecimal. It is C, which is hexadecimal value for decimal 12 (remember: 9=>9, 10=>A, 11=>B, 12=>C, 13=>D, 14=>E, 15=>F).

In other situation you could be dealing with 2-complemented numbers, and that would be completely different.

Or floating-point numbers, another kind of beast.

Same values, different meanings. How do we pick correct interpretation?

Well, it depends on context: depending on what program you'll be writing, you'll know how to interpret data.

By the way, I had to edit your code a little, here is mine:

    org $400

arr:     dc.w     1, 2, 12, 4, 21
n:     dc.w     5
sum:     dc.w     0


    org $1800

START:
    clr.l     d0        ; i = 0, we use the .l since i takes part in .l calculation
    clr.l     d3        ; sum = 0 (temporarily sum lives in d3)
    move.w    n, d1     ; d1 = n

loop:   cmp.w     d1, d0
    bge       endloop   ; if (i >= n) goto endloop

    movea.l   #arr, a0  ; a0 = &arr[0]
    adda.l    d0, a0   
    adda.l    d0, a0    ; a0 = &arr[0] + 2 x i
    add.w     (a0), d3  ; d3 = d3 + arr[i]

    addq.w    #1, d0    ; i = i + 1
    bra       loop      ; repeat loop

endloop:    move.w    d3, sum   ; write the sum into memory

    END START

Pastebin link: http://pastebin.com/Ae0CgFrt

Hope it helps,

Snoopy

znpy
  • 474
  • 2
  • 10