Convert the following program into assembly. The main method should be in one mo
ID: 3914522 • Letter: C
Question
Convert the following program into assembly. The main method should be in one module, and the factorial and power procedures in another. Do NOT use global variables (ie, a data section), unless defined globally in the pseudo-code below. Pass parameters via the stack only. Return values should use eax. Remember to preserve the register values INSIDE your functions.
String intPrompt = "Please enter an integer value: "
String hexPrompt = "Please enter a hex value: "
String error = "Value too large for multiplication"
main() {
Display(intPrompt)
unsigned int a = ReadInt()
Display(hexPrompt)
unsigned int b = ReadHex()
unsigned int result = factorial(a)
if ( result == -1 )
Display(error)
else
Display(result)
unsigned int power = power( a, b )
Display(power)
}
unsigned int factorial( unsigned int n ) {
unsigned int result = 1;
while ( n > 0 ) {
result *= n;
if ( result > 32-bit max value ) {
return -1;
}
--n;
}
return result;
}
unsigned int power( unsigned int base, unsigned int exp ) {
if ( exp == 0 )
return base;
return base * power( base, exp - 1 );
}
Explanation / Answer
segment .data
;declaring string data
intMsg db 'Please enter an integer value:'
hexMsg db 'Please enter an hex value:'
errMsg db 'Value too large for multiplocation'
len_intMsg equ $ - intMsg
len_hexMsg equ $ - hexMsg
len_errMsg equ $ - errMsg
segment .bss
;declaring variables to store integer and hex values
numInt resb 2
numHex resb 2
segment .text
global _start
_start:
;displaying prompt for integer
mov eax, 4
mov ebx, 1
mov ecx, intMsg
mov edx, len_intMsg
int 0x80
;reading integer from user input
mov eax, 3
mov ebx, 2
mov ecx, numInt
mov edx, 2
int 0x80
;showing error message if value is too large
JO ERROR_LARGE
;displaying prompt for hexadecimal
mov eax, 4
mov ebx, 1
mov ecx, hexMsg
mov edx, len_hexMsg
int 0x80
;reading hexadecimal from user input
mov eax, 3
mov ebx, 2
mov ecx, numHex
mov edx, 2
int 0x80
;showing error message if value is too large
JO ERROR_LARGE
;performing factorial of integer
Factorial:
mov al, numInt ;copying integer to accumulator
mov [res], numInt ;copying integer to res
loop_F:
dec res
JZ Factorial_complete ;see if res = 0 after decrement
mul al, res ;multiplying al and res in loop
JMP loop_F ;looping
;displying output after factorial is completed
Factorial_complete:
mov res, al
mov eax, 3
mov ebx, 2
mov ecx, res
mov edx, 2
int 0x80
;performing power of integer to hexadecimal
Power_AB:
mov eax, numInt ;copy integer to eax
mov ebx, numHex ;copying hexadecimal to ebx
cmp numHex, 0 ;checking id hexadecimal = 0
JE Power_Zero ;if 0, dispay 1
;performing power operation if hexadecimal not 0
LOOP_POW:
mul eax,eax ;multiplying eax and eax in loop
dec ebx ;decrement ebx
JNZ LOOP_POW ;looping
;print result
mov [res],eax
mov eax, 4
mov ebx, 1
mov ecx, res
mov edx, 2
int 0x80
JMP EXIT
Power_Zero:
;print 1
mov [res], 1
mov eax, 4
mov ebx, 1
mov ecx, res
mov edx, 2
int 0x80
JMP EXIT
ERROR_LARGE:
mov eax, 4
mov ebx, 1
mov ecx, errMsg
mov edx, len_errMsg
int 0x80
EXIT:
mov eax, 1
mov ebx, 0
int 0x80