+1 (315) 557-6473 

Addition of 2 Array using Assembly Language Homework Solution


Addition of Array

Programming assignment 1

Create an assembly language program that will have two arrays, of five elements each.

Sum the corresponding elements in the two arrays together and store the result in the ‘a’ array. Do this for each element. In other words:

a[0] = a[0] + b[0]

a[1] = a[1] + b[1]

a[2] = a[2] + b[2]

a[3] = a[3] + b[3]

a[4] = a[4] + b[4]

You must use one register to load the ‘a’ values into and one register to load the ‘b’ values into. And you must use a loop to load and add together each pair of ‘a’ and ‘b’ values.

To complete the Assembly Language homework, add together all of the values in the ‘a’ array, and store that value in memory.

              The values in each array at the start should be: 1, 2, 3, 4,5: a[0] = b[0] = 1

a[1] = b[1] = 2

a[2] = b[2] = 3

a[3] = b[3] = 4

a[4] = b[4] = 5

The code that adds the two arrays together and then sums the elements together is to be in a separate sub-routine.

Programming assignment 2

Create a program that will display a 2-digit number in the output window of ARC tools.

ARC IO can only send one character at a time to the output window. So, you will need to parse the number into individual digits.

To parse the number, the number (stored in a variable in memory) will have to be brought into the CPU registers and separate the number in tens position and number in one’s position. Then code will send the number in the tens position on the output window followed by the number in the one's position

Expected Output:

To display 75 in the output window:

array output 1

To display 5 in the output window:

array output 2

Solution:

Program 1.asm:

.begin .org 2048 ! start program at address 2048 a_start .equ 3008 ! set start address of array a b_start .equ 3028 ! set start address of array b ld [addr_a], %r1 ! load a address in r1 ld [addr_b], %r2 ! load b address in r2 ld [length], %r3 ! load length in r3 call add_arrays ! call sub-routine to add arrays ld [addr_a], %r1 ! load a address in r1 ld [length], %r2 ! load length in r2 call sum_array ! sum all elements of array a st %r3, [sum] ! save sum in memory halt ! halt program ! Subroutine to add arrays ! %r1 = start address of first array ! %r2 = start address of second array ! %r3 = length of both arrays (they are the same length) ! saves the sum in first array add_arrays: ld [%r1 + %r0], %r4 ! load value from a ld [%r2+ %r0], %r5 ! load value from b addcc %r4, %r5, %r4 ! add values st %r4, [%r1 + %r0] ! save result in a addcc %r1, 4, %r1 ! advance position in array a addcc %r2, 4, %r2 ! advance position in array b addcc %r3, -1, %r3 ! decrement length bne add_arrays ! repeat while not zero jmpl %r15 + 4, %r0 ! return to caller ! Subroutine to sum all elements in array ! %r1 = start address of array to sum ! %r2 = length of array ! Returns sum in %r3 sum_array: andcc %r0, %r0, %r3 ! start sum in zero sum_loop: ld [%r1 + %r0], %r4 ! load value from array addcc %r4, %r3, %r3 ! add value to sum addcc %r1, 4, %r1 ! advance position in array addcc %r2, -1, %r2 ! decrement length bne sum_loop ! repeat while not zero jmpl %r15 + 4, %r0 ! return to caller length: 5 ! length of a and b arrays addr_a: a_start ! start address of a addr_b: b_start ! start address of b .org a_start ! start of array a a: 1 2 3 4 5 .org b_start ! start of array b b: 1 2 3 4 5 sum: 0 .end

Program 2.asm:

.begin num_start .equ 3008 ! set start address of number BASE .equ 0x3fffc0 !Starting point of the memory mapped region COUT .equ 0x0 !0xffff0000 Console Data Port COSTAT .equ 0x4 !0xffff0004 Console Status Port. .org 2048 ! start program at address 2048 ld [number], %r1 ! load number in r1 andcc %r0, %r0, %r2 ! start with tens position in 0 div_loop: addcc %r1, -10, %r3 ! subtract 10 from number bneg div_end ! if < 0, end addcc %r2, 1, %r2 ! else, increment tens position addcc %r3, %r0, %r1 ! save remainder in r1 ba div_loop ! repeat loop div_end: sethi BASE, %r3 ! load base address of mmio in r3 ! wait to print first digit Wait1: ldub [%r3+COSTAT], %r4 ! load status for console andcc %r4, 0x80, %r4 ! test ready bit be Wait1 ! if not ready, wait addcc %r2, 48, %r2 ! convert digit to ascii stb %r2, [%r3+COUT] ! Print to console tens digit ! wait to print second digit Wait2: ldub [%r3+COSTAT], %r4 ! load status for console andcc %r4, 0x80, %r4 ! test ready bit be Wait2 ! if not ready, wait addcc %r1, 48, %r1 ! convert digit to ascii stb %r1, [%r3+COUT] ! Print to console ones digit halt ! halt program .org num_start ! address of number number: 5 ! number to print .end