I don’t think there is one correct answer for this, but I don’t understand how t
ID: 3877230 • Letter: I
Question
I don’t think there is one correct answer for this, but I don’t understand how to write the Little Man Computer code.
The instruction is to convert a base 10 number to binary.
The range for the input is 0 to 63. Anything above 63 would end the program.
The following are instructions from the instructor. It doesn’t make sense to me but I’m hoping someone with experience with LMC can create some code.
We need to understand that for an output of the LMC you get a max of 3 decimal numbers. So, what if 5 digits were needed, say 12345? There would have to be 2 output statements: MSB and LSB. (most significant digit and least significant digit).
So MSB => 012 and LSB => 345. In the project, we want binary out 9 digits. So MSB, Middle, and LSB.
53 Decimal is: MSB => 110, and LSB => 101
Code may look like:
input
sto TMP_IN ; get number to convert
sub six ; subtract 32
brp cal_1 ; branch around
br nxt_1 ; branch around
cal_1 sto TMP
DAT six 032
Explanation / Answer
SOLUTION:
AS you have mentioned that you need answer in LMC .
as LMC can be programmed in either machine code or assembly code.
so we are offering answer in assembly 8086 code .
N.B : Following code is compiled and executed in assembly 8086 platform.
N.B for correct output please execute this code in assembly 8086 .
array dw 7 dup(?) ; The array of binary numbers
two dw 2 ; constant value as 2
num dw ? ; input number
ten dw 10 ; Defining constant ten for extracting the digits
msg1 dw 10, 13, “Enter a number: $ ”
print_msg dw 10,13,” Binary number is :$”
counter dw 0
data_seg ends
code_seg segment
assume cs: code_seg, ds: data_seg
print_string proc ; Procedure to print a string on monitor screen
pop si ; Save Top of Stack(ToS) content (Return Address) in SI register
pop dx
mov ah, 9 ; Put the service code for printing string on monitor screen
int 21h ; Invoke the Software Interrupt 21H
push si ; Push the saved Return Address onto the stack
ret ; Return to Main Program
print_string endp
read_char proc ; Procedure to read a character from key board
pop di ; Save top of stack content (Return Address) in DI register
mov ah, 1 ; Put service code for reading a character from keyboard
int 21h ; Invoke the Software Interrupt
mov ah, 0
push ax ; Push read character onto stack
push di ; Push the saved Return Address onto the stack
ret ; Return to the main program
read_char endp
read_number proc ; Procedure to read a number
pop si ; Pop the ToS content (the Return Address) into SI
mov bx,0 ; Initialize bx, dx
mov dx,0
next_digit:
call read_char
pop ax ; Get the read character in AX from ToS
cmp al, 0Dh ; Check if the input character is CR indicating end of the number
je done
sub al, 30h ; Convert ASCII to binary
mov cl, al ; Save the digit value in CX as a word
mov ch, 0 ;
mov ax, bx ; Get back the saved partial MS digits value to AX
mul ten ; Shift left the partial MS digits value by 1 digits
add ax, cx ; Add the last digit to the shifted MS digits value
mov bx, ax ; Save the new partial MS digits value in BX
jmp next_digit ; loop back read the next digit
done: push bx ; Push the read number onto the stack
push si ; Push the Return Address back on the ToS
ret ; Return to the main program
read_number endp
start:
mov ax, data_seg ; Initialize DS
mov ds, ax
push offset msg1 ; Print the Input Prompt Message on Screen
call print_string
call read_number ; Call the number read procedure.
pop dx ; Take the number into dx from ToS.
mov num, dx ; Place the input number in the num variable
mov bx,two
lea di,array
repeat_step:
mov ax,num
cmp ax,1
je last_step
div bx
mov num,ax
mov ax,0
mov ax,dx
mov [di],ax
add di,2
inc counter
jmp repeat_step
printing_part:
lea dx ,print_msg
mov ah,9
int 21h
mov cx,0
repeat_digit:
cmp cx,0
je stop
mov dx,[di]
mov ah,2
int 21h
sub di,2
inc cx
jmp repeat_digit
stop:
mov ah, 4ch ; Enter the service code to AH register
mov al, 0 ; Set return status of the program
int 21h ; Invoke the ISR to return the control to the Operating System
code_seg ends
end start
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.