Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the following source code, M and N are constants declared with #define. 1 int

ID: 3770205 • Letter: I

Question

In the following source code, M and N are constants declared with #define. 1 int mat1[M][N]; 2 int mat2[N][M]; 3 4 int sum_element(int i, int j) { 5 return mat1[i][j] + mat2[j][i]; 6 } In compiling this code, armcc.exe produces the following code. 1 sum_element PROC 2 ADD r2,r0,r0,LSL #1 3 LDR r3,|L0.40| 4 ADD r2,r2,r0,LSL #3 5 ADD r2,r3,r2,LSL #2 6 ADD r3,r3,#0x108 7 LDR r2,[r2,r1,LSL #2] 8 ADD r1,r1,r1,LSL #1 9 ADD r1,r3,r1,LSL #3 10 LDR r0,[r1,r0,LSL #2] 11 ADD r0,r0,r2 12 BX lr 13 ENDP 14 15 DCW 0x0000 16 |L0.40| 17 DCD ||.bss|| 18 19 AREA ||.arm_vfe_header||, DATA, READONLY, NOALLOC, ALIGN=2 20 21 DCD 0x00000000 22 23 AREA ||.bss||, DATA, NOINIT, ALIGN=2 24 25 mat1 26 % 264 27 mat2 Name: Student No.: 3 28 % 264 29 30 EXPORT sum_element [CODE] 31 EXPORT mat1 [DATA,SIZE=264] 32 EXPORT mat2 [DATA,SIZE=264] 33 34 ;... some stuff omitted... 35 END 36 Reverse engineer the assembly code to determine the values of M and N. Comment the assembly to show your understanding of the code. (Hint: line 2 loads r3 with the address of mat1, and line 6 loads r3 with the address of mat2.)

Explanation / Answer

clear BL
                      ; initialize loop counter
                   ; set input function

                        ; jump label
                          ; read a digit
                 ; compare digit with carriage return
                         ; jump to label @END if carriage return
                       ; convert ascii to decimal code
                    ; rotate BX to left by 1 bit
                        ; set the LSB of BX with input
    ; jump to label @INPUT

                          ; jump label

                        ; copy BL into AL
                         ; initialize loop counter

                      ; loop label
                    ; shift AL to left by i bit
                     ; rotate BL right through carry
                     ; jump to label @LOOP

                  ; load and display  
    
   

                     ; initialize loop counter
                       ; set output function

     :                     ; jump label
                         ; shift left BL by 1 bit

                        ; jump to label @ZERO if CF=0
                    ; set DL=1
              ; jump to label @DISPLAY

                         ; jump label
                   ; set DL=0

                       ; jump label
                        ; display digit
               ; jump to label @OUTPUT

                   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote