0

The following code is supposed to take two lists and put them into one big list (C[10]) and being a beginner in assembly I am unsure as to how to check if my output is correct. I tried implementing a print to check if the left half works but it only prints zero. (https://www.kvakil.me/venus/ using this website as an emulator)

    .data
A:
    .word 0,2,3,3,4
B:
    .word 24,22,21,25,23
C:
    .word 1,1,1,1,1,1,1,1,1,1,1
min:
    .word 0
.text
.globl _main   
_main:
    add x8,x8,x0 #x8=i=0
    addi x9,x9,5  #x9=5
    #start of the loop
loop:   
        bge x8,x9,exit
        add x18,x0,x8   #x18=i
        slli x18,x18,2  #x18=i*4
        addi x19,x18,20  #x19 =i*4+ 4*5
        la x20,A #x20=&A
        la x21,B #x21=&B
        la x22,C #x22=&C
        add x20,x18,x20 #x20=&A[i]
        lw x20,0(x20) #x23=A[i]                
        add x21,x19,x21 #x21=&B[i]
        lw x21,0(x21) #x24=B[i]
        add x23,x22,x19 #x23=&C[i+5]
        sw x23,0(x21) #C[i+5]=B[i]
        add x22,x18,x22 #x22=&C[i]
        sw x22,0(x20) #C[i]=A[i] 
        
        
        addi a0,x0,1
        add x20,x0,x20
        ecall

        
        addi x8,x8,1 #i=i+1
        beq x0,x0,loop
                   
exit:

I am guessing the code is supposed to print 0224 however it is only printing 0.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ZeinZebib
  • 32
  • 1
  • 4
  • Use the single step function to verify code operation. Also, you can switch the right pane to "memory" and select "jump to data" to examine your arrays. One obvious problem is the `add x21,x19,x21 #x21=&B[i]` is using `x19` instead of `x18`. – Jester Mar 02 '21 at 23:36
  • Yeah, I figured it out after a long time. Now, I know that my registers are not updating. – ZeinZebib Mar 03 '21 at 07:42

1 Answers1

0
loop:   
    bge x8,x9,exit
    add x18,x0,x8   #x18=i
    slli x18,x18,2  #x18=i*4
    addi x19,x18,20  #x19 =i*4+ 4*5
    la x20,A #x20=&A 
    la x21,B #x21=&B
    la x22,C #x22=&C
    add x20,x18,x20 #x20=&A[i]
    lw x20,0(x20) #x23=A[i]         # comment says x23, but code says x20             
    add x21,x19,x21 #x21=&B[i]
    lw x21,0(x21) #x24=B[i]         # comment says x24, but code says x21
    add x23,x22,x19 #x23=&C[i+5]
    sw x23,0(x21) #C[i+5]=B[i]      # (*B[i]) = &C[i+5] ... I don't think you want this
    add x22,x18,x22 #x22=&C[i]
    sw x22,0(x20) #C[i]=A[i]        # (*A[i]) = &C[i] ... or this
            
    addi a0,x0,1
    add x20,x0,x20
    ecall

    
    addi x8,x8,1 #i=i+1
    beq x0,x0,loop

Do like Jester says, and single step to confirm that what you think is happening is truly so — don't wait to see the final output is right/wrong, check each and every instruction using single step!  Every single instruction is an opportunity for a typo or design issue; if any one thing is wrong a program won't fully work.

Generally speaking, you are clobbering values by repurposing registers too soon, as it turns out that you still want their older values later on.

Other than that, when the comments and the code don't agree, that's something to look into.  I'd also recommend using the friendly register names, like a0, t0, s0 instead of the raw numeric ones — probably doesn't matter for this assignment, but it will if you start doing procedure/function calling.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • Thanks a lot for the helpful comments. Is there any way I can change from *B[i] = &C[i+5] to what I have indicated in the comments? – ZeinZebib Mar 03 '21 at 07:41
  • You are clobbering values by repurposing registers too soon, as it turns out that you still want their older values later on, so don't do that. Each register can only hold one value, and that will be the latest one written to the register. If you want the old value, don't repurpose the register and instead choose another register for that new use. Then you'll have access to the newly computed value and still the old value. – Erik Eidt Mar 03 '21 at 15:32