Question

Write common assembly language (RISC) programs to a) sum the first n elements of an array...

Write common assembly language (RISC) programs to a) sum the first n elements of an array A, and b) compute r = ab for unsigned integers a and b. Each program will consist of a driver and a subprogram. The drivers will 1) read one or more values from the keyboard, 2) call the subprogram, and 3) print a result. The subprograms must not: ● store into memory, ● use registers $1 – $9, or ● make system calls

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution a) Assembly Code for sum of first n element from array: DATA arraỵ DW 100 DUP (2) ;declare array maximum size is 100ca sumValue mov sum, eax isave the sum in eax register int 21H for display the sum SumValue: push ebp mex ebp, esp mov eax, 0b) Assemblv Code for r-a*b: DATA a DB DUP (?) declare a b DB DUP (?) declare b DB DUP (2) ;declare r . CODE mov ah, 4 systemCopyable Code:

a)

.DATA
array DW 100 DUP (?) ;declare array maximum size is 100
num DB DUP(?) ;declare Number
sum DB DUP(?)
.CODE
mov ah, 4 ;system call code for int read
INT 21H   
sub al, 30H   
mov num, al
mov ebx,0 ; array offset
mov ecx,[num] ;loop counter
LOOP:
mov ah, 4 ;system call code for int read
INT 21H   
sub al, 30H   
mov array[ebx], al ;save int in the array at offset ebx
addi ebx, 4 ;increment the offset by 4
dec ecx ;decrement the ecx counter
jnz LOOP ;jump to loop if not zero
push array ;push the array values
push num ;push given num
call SumValue
mov sum, eax ;save the sum in eax register
int 21H ;for display the sum
SumValue:
push ebp
mov ebp, esp
mov eax, 0 ;save value of sum
mov ebx, [ebp+4] ;load num
mov ecx, [ebp+8] ;load the array base address
loopSum:
mov edx, [ecx] ;load array values
add eax,edx ;add array element that is edx to eax
addi ecx, 4 ;increment offset by 4
dec ebx
jnz loopSum
mov esp, ebp
pop ebp
ret

b)

.DATA
a DB DUP(?) ;declare a   
b DB DUP(?) ;declare b   
r DB DUP(?) ;declare r
.CODE
mov ah, 4 ;system call code for read int a
INT 21H   
sub al, 30H   
mov a, al
mov ah, 4 ;system call code for read int b
INT 21H   
sub al, 30H   
mov b, al
push a ;push a value
push b ;push b value
call MULTIPLY ;call MULTIPLY function
mov [r], eax ;move the result r to eax
int 21H ;for display
MULTIPLY:
push ebp
mov ebp, esp
mov eax, [ebp+4] ; load b value
mov ebx, [ebp+8] ; load a value
mul eax,ebx ;for r = a* b that is eax = eax*ebx
mov esp, ebp
pop ebp
ret

Add a comment
Know the answer?
Add Answer to:
Write common assembly language (RISC) programs to a) sum the first n elements of an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT