Question: Simplify this assembly code, and make it work, but don't use interrupts. You can use conditionals, loops, no functions. msg1 db Enter a positive number
Simplify this assembly code, and make it work, but don't use interrupts. You can use conditionals, loops, no functions.
msg1 db "Enter a positive number <= 255: ", 0
msg2 db "What shall we do with your number?", 0
msg3 db "1) Print Binary Representation", 0
msg4 db "2) Print number in Reverse", 0
msg5 db "Choice: ", 0
newline db 10, 0
not_impl db "NOT IMPLEMENTED", 0
segment .bss
;
; uninitialized data is put in the bss segment
;
num resb 1
segment .text
global asm_main
asm_main:
enter 0,0 ; setup routine
pusha
; CODE START
; print message to enter a number
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, 30
int 0x80
; read user input
mov eax, 3
mov ebx, 0
mov ecx, num
mov edx, 1
int 0x80
; convert user input to decimal
sub al, 48
; print message to select an option
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, 30
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, 30
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, msg4
mov edx, 30
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, msg5
mov edx, 30
int 0x80
; read user choice
mov eax, 3
mov ebx, 0
mov ecx, num
mov edx, 1
int 0x80
; check user choice and print output accordingly
cmp al, '1'
je print_binary
cmp al, '2'
je reverse_digits
jmp not_implemented
print_binary:
; TODO: implement printing binary representation
jmp end_program
reverse_digits:
; reverse the digits of the user's number
mov bl, al ; copy user input to bl
xor al, al ; set al to zero for accumulating digits
mov ecx, 10 ; initialize ecx to 10 for division
loop1:
xor edx, edx ; clear edx for division
div byte [ecx] ; divide by 10
push dx ; push remainder to stack
cmp bl, 0 ; check if user input is zero
jne loop1 ; if not, continue division
; print reversed digits
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
pop dx ; get last digit from stack
add dl, 48 ; convert digit to ascii
mov eax, 4
mov ebx, 1
mov ecx, dx
mov edx, 1
int 0x80
loop2:
pop dx ; get next digit from stack
add dl, 48 ; convert digit to ascii
mov eax, 4
mov ebx, 1
mov ecx, dx
mov edx, 1
int 0x80
cmp esp, 0 ; check if stack is empty
jne loop2 ; if not, print next digit
; print newline and exit
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
jmp end_program
not_implemented:
; print message for not implemented option
mov eax, 4
mov ebx, 1
mov ecx, not_impl
mov edx, 30
int 0x80
jmp end_program
end_program:
; exit program
mov eax, 1
xor ebx, ebx
int 0x80
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
