Computer Science: Assembly Language for x86 Processors VS 2017 [C++] Integer_Exp
ID: 3883628 • Letter: C
Question
Computer Science: Assembly Language for x86 Processors
VS 2017 [C++]
Integer_Expression_Calculation
Using the AddTwo program from Section 3.2 as a reference, write a program that calculates the expression Res = (A + B) – (C + D), using registers and variables.
Several steps are suggested:
1. In the data segment, define variables ordered by: varA, varB, varC, varD, and Res with some integers initialized
2. In the code segment
- calculate (A + B) – (C + D)
- save the result in Res
You can copy the following to fill the blanks with your implementations. Please do not use more than six instructions.
When finished, test your program in debugger. If you define DWORD varA as 10, varB 20, varC 30, and varD 40, Res should be -40. From your VS memory windows, when you enter &varA, the result would be like this:
where 0a 00 00 00 means 10, 14 00 00 00 is 20, 1e 00 00 00 is 30, 28 00 00 00 is 40, and d8 ff ff ff is -40
TITLE Chapter 3, Problem 1 (Integer Expression.asm) : Program: ; Description: calculates Res : Student: : Date: : Class: Chapter 3, Problem 1 (A+ B) - (C +D), using registers and variables uames Hope 02/09/2015 CSCI 241 Instructor: Mr. Ding .386 model flat,stdcall stack 4096 ExitProcess proto, dwExitcode: dword .data : define variables varA, varB, varC, varD, and Res . code inl proc ; calculate Res (A + B) - (C + D) : calculate (A B)(C D) ; save the result in Res invoke ExitProcess,O mainl endp end mainlExplanation / Answer
DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables. for the above programm you need to declare 5 variable varA, varB, varC, varD and res to store result of a program.
DATA SEGMENT
varA DW 10
varB DW 20
varC DW 30
varD DW 40
Res DW ?
ENDS
in above data segment we initialized varA to 10, varB to 20, varCto 30 and varC to 40.
for the variable Res we defined it as Res DW ?, question mark stands for value of register is not initialized.
Selection of data type is DW data type, DW stants for Define Word (Size 2 Byte).
In Assembly programming, the variable are all defined by bytes only.
DB – Define Byte (Size – 1 Byte)
DW – Define Word (Size – 2 Byte)
DD – Define Double word (Size - 4 Bytes)
DQ – Define Quad word (Size – 8 Bytes)
DT – Define Ten Bytes (Size – 10 Bytes)
Now lets move towords the CODE SEGMENT
we need to solve (A+B)-(C+D) equation in only 6 steps and store the result in Res variable
After Assuming DATA and CODE Segment, Still it is mandetory to initialize Data Segment to DS register. keyword MOV is used to move second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and after that from AX to DS. AX is the first and most important register in the ALU unit.
ADD & SUB keywords is used for addition and substraction of numbers respectively here are the some rules/ permutations do that
First permutation :- REG , memory means Register can be added with memory.
Second permutation :- memory , REG means memory can be added with Register.
Third permutation :- REG, REG means Register can be added with Register.
Fourth permutation :- memory , immediate means memory can be added with immediate.
Fifth permutation :- REG, immediate means Register can be added with immediate.
the last two line in code segment is used to exit to dos or exit to operating system. Standard Input and Standard Output related Interupts are available in INT 21H which is also called as DOS interrupt.
So our final code will be like this, after each line we will add comments for understanding purpose
DATA SEGMENT
varA DW 10 ; declare and intialize variable varA with value 10
varB DW 20 ; declare and intialize variable varB with value 20
varC DW 30 ; declare and intialize variable varC with value 30
varD DW 40 ; declare and intialize variable varD with value 40
Res DW ? ; declare variable Res with no initialization
ENDS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.