This program accepts an integer typed in by the user, verifies that the number i
ID: 3713297 • Letter: T
Question
This program accepts an integer typed in by the user, verifies that the number is valid, and if it is valid prints the binary version of the number to the display as on the following page. Call the program part2.asm. As you can see the program rejects any input which doesn't start with a+ or - sign and prints "The input is invalid."to the display. Also any integer values less than -511 or greater than +511 are rejected. Values like +0123 are acceptable When a value is rejected the program loops back and asks for a new number. printing out the binary representation of the entered number, with a between each pair of bits.Explanation / Answer
CPP Function
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{ char str[6]; // NUMBER IN STRING FORMAT.
int num; // NUMBER IN NUMERIC FORMAT.
char bin[33] = " "; // BUFFER FOR ONES AND ZEROES.
cout << "Enter an integer between -511 and +511: ";
cin >> str; // CAPTURE NUMBER AS STRING.
If ( str >= 511 && str <= -511)
{
cout << "The input is invalid ";
}
else {
num = atoi(str); // CONVERT STRING TO NUMBER.
cout << bin;
return 0;
}
}
------------------------------------------------------------------------------Assembly function
mov eax, num // THE NUMBER.
lea edi, bin // POINT TO VARIABLE "BIN".
mov ecx, 32 // NUMBER IS 32 BITS.
conversion:
shl eax, 1 // GET LEFTMOST BIT.
jc bit1 // IF EXTRACTED BIT == 1
mov [edi], '0'
jmp skip
bit1:
mov [edi], '1'
skip :
inc edi // NEXT POSITION IN "BIN".
loop conversion
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.