Question

In: Computer Science

The language is Assembly Language. Thank you! Assignment 3: Run the following code. Record what each...

The language is Assembly Language. Thank you!

Assignment 3:

Run the following code.

Record what each of the flag changes are for each command.


INCLUDE Irvine32.inc

.data

.code

main PROC

mov al,255

add al,1

call DumpRegs

sub al,1

call DumpRegs

sub al,1

call DumpRegs

exit

main ENDP

END main

Solutions

Expert Solution

INCLUDE Irvine32.inc

.data

.code

main PROC

comment: - adding 1 to 255 rolls AL over to zero:

mov al,255;

add al,1; AL=0, CF=1( unsigned overflow)

call DumpRegs

subtracting larger number from smaller:

sub al,1

call DumpRegs

sub al,1; -- AL=255, CF=1

call DumpRegs

exit

main ENDP

END main

comments:--

There are two flags used to track when a value overflows.

One is the unsigned overflow, CF, and the other is the signed overflow OF.

The CF flag is set when you do an addition that goes over the maximum value a register can hold. In your case any addition that goes over 255.

For subtractions, same thing, the other way around. If you subtract and it goes under 0, then you get a carry.

The CF flag allows you to do additions on very large numbers without the need for specialized code:

add eax, ebx

adc ecx, edx

This adds two 32 bit numbers together (eax and ebx) and then another set of 32 bit numbers (ecx, edx) taking the carry in account. The result is ebx/edx representing a 64 bit number. You can use that for numbers of any size (i.e. you could write code to add two numbers of 1024 bits each.)

If your number is unsigned, you could add a jc overflow at the end, if it jumps, then you have an overflow (your number requires 65 bits after the addition.)

The OF flag is always set, however, it is generally only used on the last addition to know whether you had an overflow in the event your number is signed. If your number is always unsigned, then OF is never used in your code (obviously, the processor always set OF no matter what since it does not know whether you are working with a signed or unsigned number.)

So in our previous addition, you would do:

add eax, ebx

adc ecx, edx

jo overflow

When the jump to overflow happens, your 64 bit addition does not fit a 64 bit number. You would need 65 bits. That can be used to generate an exception and let the user know that his math is incorrect. The OF flag, of course, would work on AL in which case that value is viewed as a number between -128 and +127. If an add generates a number larger than 127, or a subtract a number smaller than -128, then OF gets set.


Related Solutions

Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
Translate the following C code into M4K assembly language. You do not have to use the...
Translate the following C code into M4K assembly language. You do not have to use the frame pointer, just use $sp if you need to use the stack. You do not have to show the stack initialization nor stack cleanup. If you need a specific value for an address, just make an assumption. int A; main() { int B = 5; B = A+B }; // main //Disassembly starts here !main() { //stack and frame pointer init // you do...
4. Explain what is happening on each line of the following AVR assembly code. If you...
4. Explain what is happening on each line of the following AVR assembly code. If you were to execute this code what would be the final decimal values in R20, R21 and SREG registers? BCLR 0 BCLR 1 BCLR 2 BCLR 3 BCLR 4 BCLR 5 BCLR 6 BCLR 7 LDI ​R19, 0x02 LDI​R20, 0x74 LDI​R21, 0x04 LDI​R22, 0x22 ADD​R20, R22 SUB​R22, R21 ADD​R20, R21 MOV​R20, R21 JMP​DONE ADD​R21, R20 SUB​R21, R22 DONE:​SUB​R20, R21 -embedded system-
How would I code the following in assembly language? Use the Keil programming environment to code...
How would I code the following in assembly language? Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence: 1. On for 50mS, 2. Off for 400mS, 3. On for 50mS, 4. Off for 1.5S, 5. Repeat the sequence...
HCS12 Assembly code please. Translate the following code into assembly. Allocate each variable on the stack....
HCS12 Assembly code please. Translate the following code into assembly. Allocate each variable on the stack. Simulate your program and screenshot the final value of the variables in memory. { char A,B,C; int F; A = 2; B = 6; C = - 10; F = (A + B)*C; C = F +10 }
You must write the code in 80x86 assembly language and must attach a screenshot of the...
You must write the code in 80x86 assembly language and must attach a screenshot of the output. # Pennies for Pay Write a program that calculates how much a person earns in a month if the salary is one penny the first day, two pennies the second day, four pennies the third day, and so on with the daily pay doubling each day the employee works. The program should ask the user for the number of days the employee worked...
Using MIPS (MARS) - Assembly Language Assignment ( PLEASE USE COMMENTS TO DESCRIBE EACH STEP )...
Using MIPS (MARS) - Assembly Language Assignment ( PLEASE USE COMMENTS TO DESCRIBE EACH STEP ) Take input of name, the input of hours worked, and input of hourly wage, and use input of hourly wage and hours worked to calculate total paycheck and print Name, and paycheck.
PLEASE USE ASSEMBLY LANGUAGE The database students.db contains student records. Each record contains a name (30B...
PLEASE USE ASSEMBLY LANGUAGE The database students.db contains student records. Each record contains a name (30B ASCII), student ID (9B ASCII), 3 grades out of 100 (3B integers), and a letter grade (1B ASCII). For the file students.db described above, assume all records are ordered alphabetically based on the name field. A record for a new student has been entered and stored in memory. Determine how to insert the new record into the database (Hint: use string compares to determine...
Write hack assembly language code for eq lt gt
Write hack assembly language code for eq lt gt
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT