Question

In: Computer Science

MARS: Use MASKING to convert ASCII characters to Integer Write and run a program that reads...

MARS: Use MASKING to convert ASCII characters to Integer

Write and run a program that reads in a string of ASCII characters and converts them to Integer numbers stored in an array USING MASKING, not subtraction.

Write a program that:

1. Inputs a 1x8 vector of single-digit integers

2. Stores them into an 8-entry 32-bit Integer array, “V”.

After storing the integers in the array:

1. Read the same values using Read Integer and store them in a 32-bit integer array, “VPrime”.

2. Subtract the two arrays integer by integer and put the results into a third 32-bit integer array, “VCheck”.

3. Sum all the values in VCheck and using Write Integer, display the result.

When you run the program, the input should look something like this with a space between numbers:

Input V: 1 4 0 2 7 3 8 4

Input VPrime:

1

4

0

2

7

3

8

4

Check Result: 0

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The code should be similar to this: https://www.chegg.com/homework-help/questions-and-answers/need-assembly-language-mips-write-program-inputs-1x8-vector-single-digit-integers-stores-8-q40818237

But it should use MASKING to convert the ASCII characters to integers instead of subtraction.

The ASCII -> int conversion happens in the first loop in that code, on line "add $t3,$t3,-48"

Solutions

Expert Solution

1.BASICS OF MASKING:

In computer science, a mask or bitmask is data that is used for bitwise operations, particularly in a bit field. Using a mask, multiple bits in a byte, nibble, word etc. can be set either on, off or inverted from on to off (or vice versa) in a single bitwise operation.

Masking bits to 1:

To turn certain bits on, the bitwise OR operation can be used, following the principle that Y OR 1 = 1 and Y OR 0 = Y. Therefore, to make sure a bit is on, OR can be used with a 1. To leave a bit unchanged, OR is used with a 0.

Example: Masking on the higher nibble (bits 4, 5, 6, 7) the lower nibble (bits 0, 1, 2, 3) unchanged.

    10010101   10100101
 OR 11110000   11110000
  = 11110101   11110101

Masking bits to 0:

More often in practice bits are "masked off" (or masked to 0) than "masked on" (or masked to 1). When a bit is ANDed with a 0, the result is always 0, i.e. Y AND 0 = 0. To leave the other bits as they were originally, they can be ANDed with 1, since Y AND 1 = Y.

Example: Masking off the higher nibble (bits 4, 5, 6, 7) the lower nibble (bits 0, 1, 2, 3) unchanged.

    10010101   10100101
AND 00001111   00001111
  = 00000101   00000101

Querying the status of a bit:

It is possible to use bitmasks to easily check the state of individual bits regardless of the other bits. To do this, turning off all the other bits using the bitwise AND is done as discussed above and the value is compared with 1. If it is equal to 0, then the bit was off, but if the value is any other value, then the bit was on. What makes this convenient is that it is not necessary to figure out what the value actually is, just that it is not 0.

Example: Querying the status of the 4th bit

    10011101   10010101
AND 00001000   00001000
  = 00001000   00000000

Toggling bit values:

So far the article has covered how to turn bits on and turn bits off, but not both at once. Sometimes it does not really matter what the value is, but it must be made the opposite of what it currently is. This can be achieved using the XOR (exclusive or) operation. XOR returns 1 if and only if an odd number of bits are 1. Therefore, if two corresponding bits are 1, the result will be a 0, but if only one of them is 1, the result will be 1. Therefore inversion of the values of bits is done by XORing them with a 1. If the original bit was 1, it returns 1 XOR 1 = 0. If the original bit was 0 it returns 0 XOR 1 = 1. Also note that XOR masking is bit-safe, meaning that it will not affect unmasked bits because Y XOR 0 = Y, just like an OR.

Example: Toggling bit values

    10011101   10010101
XOR 00001111   11111111
  = 10010010   01101010

Arguments to functions:

In programming languages such as C, bit fields are a useful way to pass a set of named boolean arguments to a function. For example, in the graphics API OpenGL, there is a command, glClear() which clears the screen or other buffers. It can clear up to four buffers (the color, depth, accumulation, and stencil buffers), so the API authors could have had it take four arguments. But then a call to it would look like

 glClear(1,1,0,0); // This is not how glClear actually works and would make for unstable code.

which is not very descriptive. Instead there are four defined field bits, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_ACCUM_BUFFER_BIT, and GL_STENCIL_BUFFER_BIT and glClear() is declared as

 void glClear(GLbitfield bits);

Then a call to the function looks like this

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Inverse masks:

Masks are used with IP addresses in IP ACLs (Access Control Lists) to specify what should be permitted and denied. To configure IP addresses on interfaces, masks start with 255 and have the large values on the left side: for example, IP address 209.165.202.129 with a 255.255.255.224 mask. Masks for IP ACLs are the reverse: for example, mask 0.0.0.255. This is sometimes called an inverse mask or a wildcard mask. When the value of the mask is broken down into binary (0s and 1s), the results determine which address bits are to be considered in processing the traffic. A 0 indicates that the address bits must be considered (exact match); a 1 in the mask is a "don't care". This table further explains the concept.

Mask example:

network address (traffic that is to be processed) 10.1.1.0

mask 0.0.0.255

network address (binary) 00001010.00000001.00000001.00000000

mask (binary) 00000000.00000000.00000000.11111111

Based on the binary mask, it can be seen that the first three sets (octets) must match the given binary network address exactly (00001010.00000001.00000001). The last set of numbers is made of "don't cares" (.11111111). Therefore, all traffic that begins with 10.1.1. matches since the last octet is "don't care". Therefore, with this mask, network addresses 10.1.1.1 through 10.1.1.255 (10.1.1.x) are processed.

Subtract the normal mask from 255.255.255.255 in order to determine the ACL inverse mask. In this example, the inverse mask is determined for network address 172.16.1.0 with a normal mask of 255.255.255.0.

255.255.255.255 - 255.255.255.0 (normal mask) = 0.0.0.255 (inverse mask)

ACL equivalents:

The source/source-wildcard of 0.0.0.0/255.255.255.255 means "any".

The source/wildcard of 10.1.1.2/0.0.0.0 is the same as "host 10.1.1.2"

HASH TABLES:

#include <stdint.h>
#include <string.h>

int main(void) {
    const uint32_t NUM_BUCKETS = 0xFFFFFFFF;  // 2^32
    const uint32_t MAX_RECORDS = 1<<10;  // 2^10
    const uint32_t HASH_BITMASK = 0x3FF;  // (2^10)-1

    char **token_array = NULL;
    // Handle memory allocation for token_array…

    char token[] = "some hashable value";
    uint32_t hashed_token = hash_function(token, strlen(token), NUM_BUCKETS);

    // Using modulo
    size_t index = hashed_token % MAX_RECORDS;

    // OR

    // Using bitmask
    size_t index = hashed_token & HASH_BITMASK;

    *(token_array+index) = token;

    // Free the memory from token_array …
    return 0;
}

PROGRAMMING CODE IN JAVA FOR MAKING:

package com.model;

import com.mask.object.MaskData;

public class AccountDetail extends MaskData {

private String firstName;

private String lastName;

private AddressDetail address;

private CreditCardDetail creditCardDetail;

public AccountDetail(String firstName, String lastName, AddressDetail address, CreditCardDetail creditCardDetail) {

    super();

    this.firstName = firstName;

    this.lastName = lastName;

    this.address = address;

    this.creditCardDetail = creditCardDetail;

}

public String getFirstName() {

    return firstName;

}

public void setFirstName(String firstName) {

    this.firstName = firstName;

}

public String getLastName() {

    return lastName;

}

public void setLastName(String lastName) {

    this.lastName = lastName;

}

public AddressDetail getAddress() {

    return address;

}

public void setAddress(AddressDetail address) {

    this.address = address;

}

public CreditCardDetail getCreditCardDetail() {

    return creditCardDetail;

}

public void setCreditCardDetail(CreditCardDetail creditCardDetail) {

    this.creditCardDetail = creditCardDetail;

}

}

OUTPUT:

Address Class not having any confidential information

PROGRAMMING CODE:

package com.model;

import com.mask.object.MaskData;

public class AddressDetail extends MaskData{

private String addressLine1;

private String city;

private String state;

private String pincode;

private String country;

public AddressDetail(String addressLine1, String city, String state, String pincode, String country) {

    super();

    this.addressLine1 = addressLine1;

    this.city = city;

    this.state = state;

    this.pincode = pincode;

    this.country = country;

}

public String getAddressLine1() {

    return addressLine1;

}

public void setAddressLine1(String addressLine1) {

    this.addressLine1 = addressLine1;

}

public String getCity() {

    return city;

}

public void setCity(String city) {

    this.city = city;

}

public String getState() {

    return state;

}

public void setState(String state) {

    this.state = state;

}

public String getPincode() {

    return pincode;

}

public void setPincode(String pincode) {

    this.pincode = pincode;

}

public String getCountry() {

    return country;

}

public void setCountry(String country) {

    this.country = country;

}

}

PROGRAMMING CODE:

package com.mask.object;

import java.lang.reflect.Field;

import java.util.Arrays;

import java.util.HashSet;

import java.util.Set;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class MaskData implements Cloneable {

    Set fieldSet=new HashSet();

    String[] spiData = { "cardNumber", "cvv", "expDate" };

    public Object clone() {

        return this;

    }

    @Override

    public String toString() {

        StringBuffer buffer=new StringBuffer();

        try

        {

        Object object=super.clone();

        printObjectStream(buffer,object);

        }

         catch (CloneNotSupportedException ex)

        {

             ex.printStackTrace();

        }

        return buffer.toString();

    }

    private void printObjectStream(StringBuffer buffer, Object object) {

        try {

            buffer.append(object.getClass().getCanonicalName()).append("[\n");

            Object value=null;

            for (Field field : object.getClass().getDeclaredFields()) {

                if(fieldSet.add(field.getName()))

                {

                    field.setAccessible(true);

                    //System.out.println(field.getName());

                    buffer.append(field.getName() + "=");

                    value = field.get(object);

                    if(value!=null)

                    {

                        if(field.getType().isArray() |field.getType().getCanonicalName().startsWith("com.model"))

                        {

                            printObjectStream(buffer,value);

                        }

                        else if (Arrays.asList(spiData).contains(field.getName())) {

                            //field.set(object, replaceDigits((String) field.get(object)));

                            buffer.append(replaceDigits((String) value) );

                        }

                        else

                        {

                            buffer.append( value );

                        }

                    }

                }

                buffer.append("\n");

            }

            buffer.append("]");

        } catch (IllegalAccessException ex) {

            ex.printStackTrace();

        }

    }

    private String replaceDigits(String text) {

        StringBuffer buffer = new StringBuffer(text.length());

        Pattern pattern = Pattern.compile("\\d");

        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {

            matcher.appendReplacement(buffer, "X");

        }

        return buffer.toString();

    }

}

OUTPUT:

com.model.AccountDetail[

firstName=Saurabh

lastName=Gupta

address=com.model.AddressDetail[

addressLine1=Noida City Center

city=Noida

state=UP

pincode=India

country=20310

]

creditCardDetail=com.model.CreditCardDetail[

cardNumber=XXXXXXXXXXXXXXXX

cvv=XXX

expDate=XX/XX

]

]

SUMMARY:

ASWM STUDENTS HOPE YOU LIKE THE ABOVE PROGRAMMING CODE.I HAVE DISCUSSED ALL THE BASICS IN THE ABOVE CODE OF MASKING AND NON MASKING.STAY CONNECTED WITH ME MOR MORE BETTER ANSWERS..ALL THE BEST STUDENTS....


Related Solutions

*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative...
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. ‘+’ and ‘-’ will appear optionally. And once they appear, they will only appear once in the first byte. If a non-digit character appears in the string, your program should stop and return -1.
Write a program in MIPS assembly language to convert an ASCII number string containing positive and...
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT