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

Can you help me print out the max and min number of the following code? .global

ID: 3607503 • Letter: C

Question

Can you help me print out the max and min number of the following code?

.global main

.func main

main:
BL _seedrand @ seed random number generator with current time
MOV R0, #0 @ initialze index variable
writeloop:
CMP R0, #10 @ check to see if we are done iterating
BEQ writedone @ exit loop if done
LDR R1, =a @ get address of a
LSL R2, R0, #2 @ multiply index*4 to get array offset
ADD R2, R1, R2 @ R2 now has the element address
PUSH {R0} @ backup iterator before procedure call
PUSH {R2} @ backup element address before procedure call
BL _getrand @ get a random number
POP {R2} @ restore element address
STR R0, [R2] @ write the address of a[i] to a[i]
POP {R0} @ restore iterator
ADD R0, R0, #1 @ increment index
B writeloop @ branch to next loop iteration
writedone:
MOV R0, #0 @ initialze index variable
readloop:
CMP R0, #10 @ check to see if we are done iterating
BEQ readdone @ exit loop if done
LDR R1, =a @ get address of a
LSL R2, R0, #2 @ multiply index*4 to get array offset
ADD R2, R1, R2 @ R2 now has the element address
LDR R1, [R2] @ read the array at address
PUSH {R0} @ backup register before printf
PUSH {R1} @ backup register before printf
PUSH {R2} @ backup register before printf
MOV R2, R1 @ move array value to R2 for printf
MOV R1, R0 @ move array index to R1 for printf
BL _printf @ branch to print procedure with return
POP {R2} @ restore register
POP {R1} @ restore register
POP {R0} @ restore register
ADD R0, R0, #1 @ increment index
B readloop @ branch to next loop iteration
readdone:
B _exit @ exit if done
  
_exit:  
MOV R7, #4 @ write syscall, 4
MOV R0, #1 @ output stream to monitor, 1
MOV R2, #21 @ print string length
LDR R1, =exit_str @ string at label exit_str:
SWI 0 @ execute syscall
MOV R7, #1 @ terminate syscall, 1
SWI 0 @ execute syscall

_printf:
PUSH {LR} @ store the return address
LDR R0, =printf_str @ R0 contains formatted string address
BL printf @ call printf
POP {PC} @ restore the stack pointer and return
  
_seedrand:
PUSH {LR} @ backup return address
MOV R0, #0 @ pass 0 as argument to time call
BL time @ get system time
MOV R1, R0 @ pass sytem time as argument to srand
BL srand @ seed the random number generator
POP {PC} @ return
  
_getrand:
PUSH {LR} @ backup return address
BL rand @ get a random number
POP {PC} @ return

.data

.balign 4
a: .skip 40
printf_str: .asciz "a[%d] = %d "
debug_str:
.asciz "R%-2d 0x%08X %011d "
exit_str: .ascii "Terminating program. "

Can you help me print out the max and min number of the following code?

.global main

.func main

main:
BL _seedrand @ seed random number generator with current time
MOV R0, #0 @ initialze index variable
writeloop:
CMP R0, #10 @ check to see if we are done iterating
BEQ writedone @ exit loop if done
LDR R1, =a @ get address of a
LSL R2, R0, #2 @ multiply index*4 to get array offset
ADD R2, R1, R2 @ R2 now has the element address
PUSH {R0} @ backup iterator before procedure call
PUSH {R2} @ backup element address before procedure call
BL _getrand @ get a random number
POP {R2} @ restore element address
STR R0, [R2] @ write the address of a[i] to a[i]
POP {R0} @ restore iterator
ADD R0, R0, #1 @ increment index
B writeloop @ branch to next loop iteration
writedone:
MOV R0, #0 @ initialze index variable
readloop:
CMP R0, #10 @ check to see if we are done iterating
BEQ readdone @ exit loop if done
LDR R1, =a @ get address of a
LSL R2, R0, #2 @ multiply index*4 to get array offset
ADD R2, R1, R2 @ R2 now has the element address
LDR R1, [R2] @ read the array at address
PUSH {R0} @ backup register before printf
PUSH {R1} @ backup register before printf
PUSH {R2} @ backup register before printf
MOV R2, R1 @ move array value to R2 for printf
MOV R1, R0 @ move array index to R1 for printf
BL _printf @ branch to print procedure with return
POP {R2} @ restore register
POP {R1} @ restore register
POP {R0} @ restore register
ADD R0, R0, #1 @ increment index
B readloop @ branch to next loop iteration
readdone:
B _exit @ exit if done
  
_exit:  
MOV R7, #4 @ write syscall, 4
MOV R0, #1 @ output stream to monitor, 1
MOV R2, #21 @ print string length
LDR R1, =exit_str @ string at label exit_str:
SWI 0 @ execute syscall
MOV R7, #1 @ terminate syscall, 1
SWI 0 @ execute syscall

_printf:
PUSH {LR} @ store the return address
LDR R0, =printf_str @ R0 contains formatted string address
BL printf @ call printf
POP {PC} @ restore the stack pointer and return
  
_seedrand:
PUSH {LR} @ backup return address
MOV R0, #0 @ pass 0 as argument to time call
BL time @ get system time
MOV R1, R0 @ pass sytem time as argument to srand
BL srand @ seed the random number generator
POP {PC} @ return
  
_getrand:
PUSH {LR} @ backup return address
BL rand @ get a random number
POP {PC} @ return

.data

.balign 4
a: .skip 40
printf_str: .asciz "a[%d] = %d "
debug_str:
.asciz "R%-2d 0x%08X %011d "
exit_str: .ascii "Terminating program. "

Explanation / Answer

Please see the below answer, a modified code is being provided out here in two coding methods.

Answer:

  There will be a very little change in the below code to get the required output as in this code the random number are taken in a stack.

     

          .global main

                  .data

                 .balign      4
a: .skip 40
printf_str: .asciz "a[%d] = %d "
debug_str:
                  .asciz "R%-2d 0x%08X %011d "

                  .func main

main:
BL _seedrand @ seed random number generator with current time
MOV R0, #0 @ initialze index variable
writeloop:
CMP R0, #10 @ check to see if we are done iterating
BEQ writedone @ exit loop if done
LDR R1, [a] @ get address of a
LSL R2, R0, #2 @ multiply index*4 to get array offset
ADD R2, R1, R2 @ R2 now has the element address
PUSH {R0} @ backup iterator before procedure call
PUSH {R2} @ backup element address before procedure call
BL _getrand @ get a random number
POP {R2} @ restore element address
STR R0, [R2] @ write the address of a[i] to a[i]
POP {R0} @ restore iterator
ADD R0, R0, #1 @ increment index
B writeloop @ branch to next loop iteration
writedone:
MOV R0, #0 @ initialze index variable
readloop:
CMP R0, #10 @ check to see if we are done iterating
BEQ readdone @ exit loop if done
LDR R1, [a] @ get address of a
LSL R2, R0, #2 @ multiply index*4 to get array offset
ADD R2, R1, R2 @ R2 now has the element address
LDR R1, [R2] @ read the array at address
PUSH {R0} @ backup register before printf
PUSH {R1} @ backup register before printf
PUSH {R2} @ backup register before printf
MOV R2, R1 @ move array value to R2 for printf
MOV R1, R0 @ move array index to R1 for printf
BL _printf @ branch to print procedure with return
POP {R2} @ restore register
POP {R1} @ restore register
POP {R0} @ restore register
ADD R0, R0, #1 @ increment index
B readloop @ branch to next loop iteration
readdone:
B _exit @ exit if done
  
_exit:  
MOV R7, #4 @ write syscall, 4
MOV R0, #1 @ output stream to monitor, 1
MOV R2, #21 @ print string length
LDR R1, exit_str @ string at label exit_str:
SWI 0 @ execute syscall
MOV R7, #1 @ terminate syscall, 1
SWI 0 @ execute syscall

_printf:
PUSH {LR} @ store the return address
LDR R0, printf_str @ R0 contains formatted string address
BL printf @ call printf
POP {PC} @ restore the stack pointer and return
  
_seedrand:
PUSH {LR} @ backup return address
MOV R0, #0 @ pass 0 as argument to time call
BL time @ get system time
MOV R1, R0 @ pass sytem time as argument to srand
BL srand @ seed the random number generator
POP {PC} @ return
  
_getrand:
PUSH {LR} @ backup return address
BL rand @ get a random number
POP {PC} @ return

exit_str: .ascii "Terminating program. "

. data
LEN DW     $-ARR
MIN             DB ?
MAX           DB ?

.code
DS:    data

CS:    code

START:

   MOV AX,data
   MOV DS,AX

LEA SI, ARR
MOV BL, ARR[SI]
MOV MIN, BL
MOV MAX, BL

MOV BX,LEN

REPEAT:

MOV BL,ARR[SI]
CMP MIN,BL
JL CheckMAX

MOV MIN,BL
Check MAX:
CMP MAX,BL
JG DONE

MOV MAX,BL
DONE:
INC SI
LOOP REPEAT

MOV AH,4CH
INT 21H
code ENDS
END START

   LI $v0, 42           # 42 is system call to generate random integer

LI AX, 100        # AX is where you set the upper bound

syscall             # generated number will be at AX

LI $v0, 1            # 1 is the system call to show an int number

syscall              # generated number is at AX,

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