Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Host byte order in a computer system is either little endian or big endian depen

ID: 3630501 • Letter: H

Question

Host byte order in a computer system is either little endian or big endian depending on the computer arhitecture. For example in Intel architecture computers the host byte order is little endian , while in Motorola computers , the host byte order is big endian. Network byte order on the other hand is always big endian. This is necessary to make sure the systems from different manufacturers are compatible on the network.
uint32_t htonl(uint32_t hostlong)
uint32_t ntohl(uint32_t netlong)

are two functions that are desired to convert from host byte order to network byte order or vice versa.

The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.
The ntohl() function converts the unsigned integer netlong from network byte order to host byte order.

Write a function htonl in assembly that will accept an integer variable and covert it from host byte order to network byte order.
Write a function ntohl in assembly that will accept an integer in network byte order and return an integer in host byte order.



Explanation / Answer

In order to convert from little endian to big endian or vice versa, we need a device to execute these functions. The following functions will be executed in that device. I assumed this device consists of intel 80486 processor. The bswap instruction available on 80486 and later processors converts between 32 bit little endian and big endian values. This instruction accepts only a single 32 bit register operand. It swaps the first byte with the fourth and the second byte with the third. The syntax for the instruction is bswap reg32 where reg32 is an 32 bit general purpose register. The bswap instruction is to provide access to a second set of 16 bit general purpose registers. If you are using only 16 bit registers in your code you can double the number of available registers by using the bswap instruction to exchange the data in a 16 bit register with highest order word of a thirty-two bit register. For example you can keep two 16 bit values in eax and move the appropriate value into ax as follows: bswap eax bswap eax bswap eax Insert the “bswap” instruction in the functions to change from little to big endian and vice versa.