Using emu8086.inc , write an assembly program that display the message
'Enter the number:'
Then, reads a byte from the user, then count the number of 1’s in it, and display them on screen.
Hello!
EXPLAINATION:
Before going to the code first we will look into some basics of 8086
AX, BX , CX and DX are 16 bit general purpose registers.
AX is also called as accumulator. any operation like division and ultiplication the result is stored in accumulator(AX)
We can have have 8 bits by refering X with L or H like AH and AL both are 8 bit registers AX = AH + AL , where H represent Higher byte L represents lower byte.
MOV operand 1, operand 2
In this instruction first operand mush be register(AX,AL,AH,BX...) or memory location([DI],[SI]) , second operand can be immediate data or register or memory location.
Ex:
MOV AX, 0015h
MOV [4000h],12h
DB , DW are directives
Directives are used to store inforamtion in the memory
Here i have taken org 100h the program is utilising after first 2 bytes so now our 1st word starts storing at 102h offset address.
var_name db value1,value2,.......value n
Byte(8bits)
In 8086 you can store the data into memory using variables here var_name is a variable so it will allocate the 1 byte memory to the values
var_name dw value1,value2,.......value n
Word(16bits or 2 bytes)
Here var_name is a variable so it will allocate the 2 bytes memory to the all the values
var_name db ?
This creates a variable with no data assigned to it so use ? when dont have clue store which value in varaible.
NOTE: values can be number or charecters or string
VAR_NAME , var_name both are same, 8086 is not case sensitive
ADD operand 1, opearand 2
This instruction add the operand 1 and operand 2 and result stores back to the operand 1.
In this instruction operand 1 must not be immediate value but it can be a register and memory location.
Operand 2 can be any thing register or memory location or register.
Ex:
ADD Cl , 05h
ADD [SI] , 45h
ADD 78h, AX ; first operand must not be immediate value
ADD [DI],Var1 ; both operands must not be addresses
Procedures:
Procedures helps us in code reusability , its similar to functions sometimes same instructions we need to execute for different data in that situation we can use procedures
SYNTAX :
PROC procedure_name
; code
RET
ENDP
INTERUPTS:
Interrupts will stop the main program and executes a sub routine(other than main program) and comes back to the main progarm and continues its execution after the interrupt.
There are so many sub routines so in order to execute the particular sub routine so value we should give to the AX register before interrupt.
There are lot of intrupts in 8086 , and to discuss here about all is out of the sope of this question.
If you want to know more about interrupts please go to the HELP section in EMU8086 then go to documentation index page there you can find interrupts
Loding offset address:
When enter is a variable declared above in DATA segments then we can get offset address in 2 ways
LEA DX,enter
MOV DX,offset enter
both are same.
STACK operations:
Stack is contiguous memory allocation we can push or pop only top of the stack .
PUSH BX means we are pushing the value in BX to stack
POP BX means we are getting the data from top stack of stack and stores in the register BX , by doing this in stack the data will be deleted and comes to register.
Algorithm used:
First i have stored the strings like 'Enter the number ', 'Number of ones are:-' and new line charecter to respentive varibles.
Count variable is used to store the number 1s in the given byte
First it will display the msg Enter the number and then it takes the bit untill 8 bits are entered.
Each time when we enter bit it will be stored as ascii format suppose we enter 1 means then 31 is pushed to stack, so we will subtract 30 and then push to the stack.
Next it will display msg number of ones are:-
And then it will count number of 1s by poping from the stack , at this part we will check whether it is 1, if it is 1 then we will increment DH , so finally DH stores the number of 1s in the byte.
Finally Displaying number of 1s occured to the screen.
CODE:
.DATA
enter DB 0DH,0AH, "ENTER THE NUMBER :-$"
number DB 0DH,0AH, "NUMBER of one's are :- $"
new_line db 0dh,0ah,"$"
count DB ?
.CODE
START:
MOV AX,@DATA
MOV DS,AX
; Displays 'Enter the number'
ENTER_MSG:
LEA DX,enter ; Offset address of enter variable
MOV AH,09H ; display subroutine
INT 21H ; call the subroutine by intrupting
MOV CL,00 ; make value in cx is 0
MOV AH,01H ; set the cursor at 1st line
CALL INPUT_BYTE ; call the input string procedure
; Displays 'Number of one's are:-'
COUNT_MSG: ; lable for displaying reverse msg
LEA DX,number ; Dx gets stored with offset address of reverse variable
MOV AH,09H ; Display subroutine
INT 21H ; call the subroutine by intrupting
lea dx, new_line ; DX gets stored with offset address of new_line variable
mov ah,09h ; Display subroutine
int 21h ; call the subroutine by intrupting
MOV DH,00h
; Counts number of 1s in given byte
COUNT_ONEs:
POP BX ; pops the last entered value and stores back to BX
MOV DL,BL ; Move BL value to DL
POP BX
CMP DL,01h ; Compare the bit wether it is 1 or not
JNZ skip ; If 1 then execute the below instruction
INC DH ; increment the DH value
skip:
LOOP COUNT_ONEs ; loop till the value in CX is 0
; Display final count of 1s to the screen
mov al,dh ; convert to binary coded decimal,
or al, 30h ; convert to printable symbol:
; print char in al using bios teletype function:
mov ah,0eh
int 10h
HLT ; HALT The program
INPUT_BYTE PROC ; procedure to take input from keyboard
INT 21H ; call the subroutine by intrupting
MOV BL,AL ; moves value in AL to BL
SUB BX,30h ; Subtract 30 as ascii value for numbers starts from 30-39
PUSH BX ; push the BX to stack
INC CL ; calculate number of bits
CMP CL,08h ; If user enters 8 bits
JZ COUNT_MSG ; then goes to count_msg
CALL INPUT_BYTE ; otherwise it calls the same procedure till user enters 8 bits
RET ; RETURN back to the caller
INPUT_BYTE ENDP ; End of procedure
.EXIT
END START
OUTPUT:
AFTER EXECUTION:
Hope this helps and clear!
Please feel free to ask any doubts in the comments.
I strive to provide the best of my knowledge so please f UpVote if you like the answer.
Thank you!
Using emu8086.inc , write an assembly program that display the message 'Enter the number:' Then, reads...
Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement. Testing For submitting, use the data in the...
2. Write an 80x86 assembly language program that reads byte size signed integers from memory and counts the number of zeros. Store this count in memory. End when you get a negative number. (20pts) For example: nums DB 4, 0, 0, 12, 6, 8, 0, 4, -1 count DB 0 after executing the procedure count should be 3 count DB 3.
IN PERL Write a program that will ask the user for a number and will print all the integers in order starting from 1 until the number. Use the <> operator to ask for the user's input. For this program you will write a countdown timer. Your time will count down from 1 minute (60 seconds) to 0. Once the timer gets to 0 seconds the system will display a message that reads: RING RING RING! -using do while-
Write an assembly language 32 bit program that reads in lines of text by a .txt file and read in from the user and prints them diagonally. Using good commenting.
Write a C program that reads a sequence of numbers and display a message 1. The numbers a re red from the standard input. 2. The first number is the length of the sequence (n) followed by n numbers. 3. If n is 0 or negative, the program displays the message "Error_1" followed 4. If the length is shorter than n, it displays "Error_2" followed by a new line 5. The program inspects the list and display one of the...
Write a C# program that prompts a user to enter a birth month and day. Display an error message if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message.
Write the program FindPatientRecords that prompts the user for an ID number, reads records from Patients.txt, and displays data for the specified record. If the record does not exist, display the following error message: No records found for p# using System; using static System.Console; using System.IO; class FindPatientRecords { static void Main() { // Your code here } }
Write the program FindPatientRecords that prompts the user for an ID number, reads records from Patients.txt, and displays data for the specified record. If the record does not exist, display the following error message: No records found for p# using System; using static System.Console; using System.IO; class FindPatientRecords { static void Main() { // Your code here } }
5. Write a program that does to perform the following using: Reads a number num that is in the range from 0 to 999999999 and a digit n that is in the range from 0 to 9 from the user. Write while loop to force user to enter appropriate values. . Finds how many times the digit n occurs in the number num. Prints the output as shown. Eİ CAWindowsisystem32cmdexe Enter number ( to 999999999): 3545592 the digit S appears...
Easy 68k code - assembly language Write a 68K program that reads two integers (>10 and <225) of the keyboard, adds them together and multiplies them, writes the numbers and the results of addition and multiplication on the screen. If the numbers are not on the range, writes an error message and requests a new number. Example of the execution: Insert the first number: 10 Insert the second number:15 The sum is: 25 The product is: 150 Please take a...