Giter Site home page Giter Site logo

Comments (6)

pjlobo avatar pjlobo commented on June 1, 2024 1

Your code is modifiying itself. The tests store their results starting at address 0x68 (104), which is where the first instruction of test 3 is located, so the instructions of test 3 are completely overwritten by tests 1 and 2. That's why you are seeing "Unknown instruction". Removing some instructions makes the program shorter and therefore the test 3 instructions aren't overwritten any more.

Assembler labeling is your friend. Here is a quick and dirty example using the store global pseudo-instruction. More elegant solutions exist that avoid lots of unnecesary "auipc"s leveraging x3/gp.

# Immediates
    li a0, 10
    li a1, -10
    li a2, 6
    li t0, 1
    li s0, 1

# Test1: mul, no mulh
while: # calculate 10^6 and (-10)^6
    beqz a2, break
    mul  t0, t0, a0
    mul  s0, s0, a1
    addi a2, a2, -1
    j    while
break:
    li a0, -1
    mul  s0, s0, a0 
    sw   t0, test1r1, t6 # 10^6    =  1.000.000 = 0x000F 4240
    sw   s0, test1r2, t6 # (-10)^6 = -1.000.000 = 0xFFF0 BDC0
    
# Test2: mul and mulh
    # Positive x positive
    mul  t1, t0, t0    # 10^6 * 10^6, lower bits
    mulh s1, t0, t0    # 10^6 * 10^6, upper bits
    sw   t1, test2r1l, t6 # 0xD4A5 1000
    sw   s1, test2r1h, t6 # 0x0000 00E8

    # Positive x negative
    mul  t2, t0, s0    # 10^6 * (-10)^6, lower bits
    mulh s2, t0, s0    # 10^6 * (-10)^6, upper bits
    sw   t2, test2r2l, t6 # 0x2B5A F000
    sw   s2, test2r2h, t6 # 0xFFFF FF17

    # Negative x negative
    mul  t3, s0, s0    # (-10)^6 * (-10)^6, lower bits
    mulh s3, s0, s0    # (-10)^6 * (-10)^6, upper bits
    sw   t3, test2r3l, t6 # 0xD4A5 1000
    sw   s3, test2r3h, t6 # 0x0000 00E8

# Test3: mul and mulhsu

    # Positive x negative, interpreted as positive
    # -1.000.000 = 0xFFF0 BDC0, so as a positive number, we have 4.293.967.296
    mul  t4, t0, s0    # 10^6 * "(-10)^6", lower bits
    mulhsu s4, t0, s0  # 10^6 * "(-10)^6", upper bits
    sw   t4, test3r1l, t6 # 0x2B5A F000
    sw   s4, test3r1h, t6 # 0x000F 4157

end:
    li a7, 10
    ecall

.data
test1r1: .word 0
test1r2: .word 0
test2r1l: .word 0
test2r1h: .word 0
test2r2l: .word 0
test2r2h: .word 0
test2r3l: .word 0
test2r3h: .word 0
test3r1l: .word 0
test3r1h: .word 0

from ripes.

pjlobo avatar pjlobo commented on June 1, 2024 1

According to section 7.1 of "The RISC-V Instruction Set Manual Volume I: Unprivileged ISA" Document version 20191213, "If both the high and low bits of the same product are required, then the recommended code sequence is: MULH[[S]U] rdh, rs1, rs2; MUL rdl, rs1, rs2 (source register specifiers must be in same order and rdh cannot be the same as rs1 or rs2)." Have you tried swapping the MUL and MULH (or MULHSU) instructions (putting MULH or MULHSU before MUL)?

That's just a recommendation that makes it easier for a microarchitecture to identify whether instruction execution can be optimized (e.g. computing simultaneously both the high and low parts of the result), but the result must be the same regardless of order.

from ripes.

jnoche avatar jnoche commented on June 1, 2024

According to section 7.1 of "The RISC-V Instruction Set Manual Volume I: Unprivileged ISA" Document version 20191213, "If both the high and low bits of the same product are required, then the recommended code sequence is: MULH[[S]U] rdh, rs1, rs2; MUL rdl, rs1, rs2 (source register specifiers must be in same order and rdh cannot be the same as rs1 or rs2)." Have you tried swapping the MUL and MULH (or MULHSU) instructions (putting MULH or MULHSU before MUL)?

from ripes.

jnoche avatar jnoche commented on June 1, 2024

Also, in one of your comments you state "# (-10)^6 = -1.000.000 = 0xFFF0 BDC0". Shouldn't (-10)^6 be positive?

from ripes.

StefMassin avatar StefMassin commented on June 1, 2024

According to section 7.1 of "The RISC-V Instruction Set Manual Volume I: Unprivileged ISA" Document version 20191213, "If both the high and low bits of the same product are required, then the recommended code sequence is: MULH[[S]U] rdh, rs1, rs2; MUL rdl, rs1, rs2 (source register specifiers must be in same order and rdh cannot be the same as rs1 or rs2)." Have you tried swapping the MUL and MULH (or MULHSU) instructions (putting MULH or MULHSU before MUL)?

Swapping the instructions seems to have no effect. I get the same behaviour as before.

Also, in one of your comments you state "# (-10)^6 = -1.000.000 = 0xFFF0 BDC0". Shouldn't (-10)^6 be positive?

Yes, almost every appearance of (-10)^6 should actually be -(10^6), that's my bad. However the program itself does generate 10^6 and -10^6 correctly, it is only the comments.

from ripes.

StefMassin avatar StefMassin commented on June 1, 2024

Your code is modifiying itself. The tests store their results starting at address 0x68 (104), which is where the first instruction of test 3 is located, so the instructions of test 3 are completely overwritten by tests 1 and 2. That's why you are seeing "Unknown instruction". Removing some instructions makes the program shorter and therefore the test 3 instructions aren't overwritten any more.

Yep, you're right, such a silly mistake. Thanks so much!

from ripes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.