Question

In: Computer Science

Question1. (lesson 2) In the following code we determine a number if its even or odd....

Question1. (lesson 2)

In the following code we determine a number if its even or odd. Explain what logic is used for that?   

section .text

   global _start            ;must be declared for using gcc

      

_start:                     ;tell linker entry point

   mov   ax,   8h           ;getting 8 in the ax

   and   ax, 1              ;and ax with 1

   jz    evnn

   mov   eax, 4             ;system call number (sys_write)

   mov   ebx, 1             ;file descriptor (stdout)

   mov   ecx, odd_msg       ;message to write

   mov   edx, len2          ;length of message

   int   0x80               ;call kernel

   jmp   outprog

evnn:  

  

   mov   ah, 09h

   mov   eax, 4             ;system call number (sys_write)

   mov   ebx, 1             ;file descriptor (stdout)

   mov   ecx, even_msg      ;message to write

   mov   edx, len1          ;length of message

   int   0x80               ;call kernel

outprog:

   mov   eax,1              ;system call number (sys_exit)

   int   0x80               ;call kernel

section   .data

even_msg db 'Even Number!' ;message showing even number

len1 equ $ - even_msg

   

odd_msg db 'Odd Number!'    ;message showing odd number

len2 equ $ - odd_msg

                    

Question 2. (lesson 1 and lesson 3)

                    Write a program that does the following(modification from assignment 3)

            1. Asks the user to enter two numbers (each is a byte) A and B

            2. Multiply the numbers and stores the result in variable C.

                  3. so that if the product  exceeds 10, the program would print “result is greater than 10”.

                      Otherwise (if result < 10), it prints the result as usual.

Solutions

Expert Solution

First question

To find whether a number a number is even or odd, we check its least significant bit in binary representaion. If the Least Significant Bit is 1, the number is odd, otherwise it is even. Here, AND operation is performed between the variable ax and 1. In the line jz evnn, it is being checked whether after performing the AND operation, the value in z is zero(0) or not. If the value of z is zero, the number is even. Otherwise it is odd.

In AND operation, the answer is one only when both the input bits are one, otherwise the answer is zero. So, the last bit of the number when combined with one through AND operation, gives one when it is one(indicating number is odd), or gives zero, when it is zero(indicating number is even).

Second Question

global_start

A DB ?
B DB ?
MSG1 DB 10,13,”ENTER FIRST NUMBER : $”
MSG2 DB 10,13,”ENTER SECOND NUMBER : $”
MSG3 DB 10,13,”Result is greater than 10 : $”

ENDS

CODE SEGMENT
ASSUME DS:DATA CS:CODE
START:
MOV AX,DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV A,AL

LEA DX,MSG2
MOV AH,9
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
MOV B,AL
MUL A
MOV C,AL
AAM
ADD AH,30H
ADD AL,30H
MOV BX,AX
CMP BX, #10
GT BX,#10 
LEA DX,MSG3
MOV AH,9
INT 21H
MOV AH,2
MOV DL,BH
INT 21H
MOV AH,2
MOV DL,BL
INT 21H
MOV AH,4CH
INT 21H
ENDS
END START
 

Related Solutions

Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
divide even number by 2 and what is the reminder? duvid odd number by 2 and...
divide even number by 2 and what is the reminder? duvid odd number by 2 and what is the reminder? here modulus is a remiander function. MY_ARRAY[] is an array if 49 elements or DINT[49]. Create four versions of structured text code that sets all even elements of MY_ARRAY[] =3 and all odd elements = 7 using. You can use any function or construct in structured text however each version will contain only 1 type of loop construct: 1) 1...
determine whether the given function is even, odd, or neither. Please write a code in MatLab...
determine whether the given function is even, odd, or neither. Please write a code in MatLab to solve this problem below: 1.f(x) = sin 3x please only use Matlab to solve this problem
Prove that every natural number is odd or even.
Prove that every natural number is odd or even.
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input...
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input value. Repeat as long as the user wishes to continue //----------------------------------------------------------------- public static void main(String[] args) {     // Declare the identifiers final int SENTINEL = -99;    // Declare the remaining identifiers ... Scanner scan = new Scanner(System.in);    // Display the programmer's information              // Input an integer number          // Count the number of odd, even, and...
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
Create an application that checks whether an integer is an odd or even number. Welcome to...
Create an application that checks whether an integer is an odd or even number. Welcome to the Odd/Even Checker! Enter an integer: ten Error! Invalid integer. Try again. Enter an integer: 10.3 Error! Invalid integer. Try again. Enter an integer: 10 The number 10 is even. Continue? (y/n): Error! This entry is required. Try again. Continue? (y/n): y Enter an integer: 9 The number 9 is odd. Continue? (y/n): n Specifications: Create a version of the Console class presented in...
Find a system of recurrence relations for the number of n-digit quaternary sequences that contain an even number of 2’s and an odd number of 3’s.
Find a system of recurrence relations for the number of n-digit quaternary sequences that contain an even number of 2’s and an odd number of 3’s. Define the initial conditions for the system. (A quaternary digit is either a 0, 1, 2 or 3)
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
1- Create a Java program to determine a certain number whether that number is odd or...
1- Create a Java program to determine a certain number whether that number is odd or even. Hint : use arithmetic operators and expressions to find the odd or even numbers. 2- Create a Java program to ask a user to enter a name and password as shown below: name is “Ahmed” and his password is 2321 or name is “Ali” and his password is 6776 . The program shows a greeting “Hi ..Welcome to my program” if the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT