Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your program should use the following opcodes: mov, movzx, add, sub and call. For clarification on how to use the above opcodes please look at

Your program should use the following opcodes: mov, movzx, add, sub and call.

For clarification on how to use the above opcodes please look at add.asm and sub.asm on the class web site.

Write a program to do the following.

1. create the following variables in the data section. Declare them as WORD (not BYTE or DWORD)

num1 (initialize to 0F9FD hex) num2 (initialize to 0FCFE hex)

In the code section write code to do the following:

1. You should populate the following registers with the following values in the following order:

ebx = 0BBBBFFFF hex eax = 0AAAAFFFF hex ecx = 0E2B5FFFD hex edx = 0FFFFFFFF hex bl = 11111010 binary bh = 251 decimal dx = FFB5 hex

2. After you populate the above registers you should evaluate the following expression:

eax = bh + bl + ecx - dx + num2 - num1

Note: you should paste the above expression in your code as a comment just before code to evaluate the expression.

You should follow the normal order of operations to evaluate the above expression. In other words work left to right and accumulate the subtotal in eax.

For example start the subtotal in eax with bh then add bl to that subtotal and then add ecx to that subtotal and then subtract dx from that subtotal add num2 to that subtotal and then subtract num1 from that subtotal to get the total.

Caution: you cannot directly add or subtract 2 variables. How do you solve this problem? (hint: look at add.asm on the class web site).

Do not add a register with a number. For example do not: add edx, 1234 etc.

You will be graded partly on efficiency which means use the fewest lines of code to get the job done.

For this assignment if you are adding registers of similar sizes the answer might not fit into the destination with the following exception: the grand total is guaranteed to fit into eax.

The solution to the fit problem is to convert the smaller type to a larger one using the movzx instruction and then do the addition or subtraction.

Also if there is a number in eax that you wish to add to or subtract from, do not add to or subtract from ax, ah or al. It is not the same as adding to or subtracting from eax.

Caution: do not unnecessarily zero out a register or part of a register.

Caution: the add and subtract instructions require the operands to be of the same size. For example you cannot directly add BH (1 byte) and EAX (4 bytes). How do you solve this problem? (hint: look at add.asm on the class web site).

Note: by default all numbers in assembly are in decimal (base 10). If you wish to designate a different base you must append the correct character to the number:

10 hex = 10h 1001 binary = 1001b 50 base 10 = 50d or just 50

Required Console output - Printing the EAX register

After calculating the total and accumulating it in eax, print the eax register to the screen by calling the Irvine function, WriteHex.

You can see an example of calling WriteHex in add.asm.

The code to call WriteHex should be just before the exit code described below.

Exit Code

At the end of your code you should have the following 2 lines of code (see add.asm on the class web site)

call ReadChar ;pause execution INVOKE ExitProcess,0 ;exit to dos: like C++ exit(0)

Registers you can use

In this program you may use registers you initialize with data in step 1 above and you may also use the esi and/or edi registers if necessary.

Do not use the ebp or the esp registers.

Caution: remember that cx is part of ecx and that. bl and bh are part of ebx . If you change ecx you may be changing cx and if you change ebx you may be changing bl and bh.

Debugging your program After fixing all compile errors you should step through your program one line at a time using F10.

You should display the registers by hitting ALT 5. You can only display the registers if your program is running and you are stepping through your code with F10.

Displaying the registers will allow you to check your subtotal as you go.

You should also display the flags by right clicking in the register window and clicking on flags.

The flag we are interested in is the CY flag (carry flag).

If the CY flag becomes 1 after you perform an addition then you know you have a carry out and that the result of an add did not fit into the destination.

If you have a carry out then this is an error for program 1 and you will have to fix your code so there are no carry outs.

See add.asm for a solution on how to add 2 numbers that will not fit into a destination (hint: convert the numbers into a larger type then do the addition).

image text in transcribed

+ add (1).asmx General form: 6 ADD operandi, operand2 ; Description: 8 operandi = operandi + operand2 9 Allowable operands: 10 reg, reg reg, mem reg, immed mem, reg mem, immed ; Flags changed: 0,5, Z,A,P, C ; Note: Both operands must be of the same size. Can be unsigned or signed binary integers. .386 ; identifies minimum CPU for this program .MODEL flat, stdcall flat protected mode program TIIVI;stdcall enables calling of MS_windows programs : allocate memory for stack ; (default stack size for 32 bit implementation is IMB without .STACK directive default works for most situations) .STACK 4096 ; allocate 4096 bytes (1999h) for stack *PROTOTYPES******* ExitProcess PROTO, dwExitCode: DWORD from Win32 api not Irvine 34 Readchar PROTO ;Irvine code for getting a single char from keyboard ; Character is stored in the al register. ; Can be used to pause program execution until key is hit. WriteHex PROTO ;Irvine function to write a hex number in EAX to the console ******DATA SEGMENT******* .data * 41 * 42 74 43 * 14 ** 45 * 46 +0 47 * 48 49 +9 50 51 31 52 allum bNum byte byte 7h ih *****CODE SEGMENT**** .code Emain PROC 54 , 00456CCh 9ABC12h , 45234Bh 1000ob 57 jebx = 0c456 Ch jeax = 9ABC12h jedx = 452348h ;bl = 10999 = 10h = 16 (base 10) jadd bl to eax ; add eax, bl ; illegal. both operands must be same size ; add eax, ebx is Legal but ebx does not contain correct number to add ;solution: use the movzx instruction jmovzx allows you to move a smaller operand into a larger one jand zeros out the upper part of the larger operand ; copy bl to dl and zero ; out upper part of edx ; add edx to eax WriteHex ; Print number in eax to the console in hex ; add ; add ax, bl eax, bx . ; illegal. both operands must be same size ; illegal. both operands must be same size , effffh ; Problem if sum will not fit into destination ;bx = fffffh. ffffh is the largest 2 byte number 1ch ;dx = 1ch jerror: sum will not fit into dx (FFFF+1C = 1991Bh but edx contains only Ibh) ; solution if sum will not fit into destination: copy both operands to a larger type using movzx ;then do addition 83 84 0+ 85 86 87 - Ich ;reinitialize dx to Ich ; copy dx to cx and zero out the upper part of ecx ; copy bx to si and zero out the upper part of esi ; add esi to ecx: ffffh+ICH =1891bh jadd 2 variables , offset bNum: get address of bum to look at bNum in memory window vadd anum, bNum; illegal. no memory to memory operations 89 90 20 91 92 22 93 94. 27 95 96 20 97 98 30 99 199 100 191 101 192 102 103 104 105 106 ito add one variable to another first copy one of them to a register , aNum bNum, ; copy anum to a register ; add al to bum and store answer back in bum Readchar ExitProcess, ; Pause program execution while user inputs a non-displayed char jexit to dos: Like C++ exit() INVOKE main ENDP END main + add (1).asmx General form: 6 ADD operandi, operand2 ; Description: 8 operandi = operandi + operand2 9 Allowable operands: 10 reg, reg reg, mem reg, immed mem, reg mem, immed ; Flags changed: 0,5, Z,A,P, C ; Note: Both operands must be of the same size. Can be unsigned or signed binary integers. .386 ; identifies minimum CPU for this program .MODEL flat, stdcall flat protected mode program TIIVI;stdcall enables calling of MS_windows programs : allocate memory for stack ; (default stack size for 32 bit implementation is IMB without .STACK directive default works for most situations) .STACK 4096 ; allocate 4096 bytes (1999h) for stack *PROTOTYPES******* ExitProcess PROTO, dwExitCode: DWORD from Win32 api not Irvine 34 Readchar PROTO ;Irvine code for getting a single char from keyboard ; Character is stored in the al register. ; Can be used to pause program execution until key is hit. WriteHex PROTO ;Irvine function to write a hex number in EAX to the console ******DATA SEGMENT******* .data * 41 * 42 74 43 * 14 ** 45 * 46 +0 47 * 48 49 +9 50 51 31 52 allum bNum byte byte 7h ih *****CODE SEGMENT**** .code Emain PROC 54 , 00456CCh 9ABC12h , 45234Bh 1000ob 57 jebx = 0c456 Ch jeax = 9ABC12h jedx = 452348h ;bl = 10999 = 10h = 16 (base 10) jadd bl to eax ; add eax, bl ; illegal. both operands must be same size ; add eax, ebx is Legal but ebx does not contain correct number to add ;solution: use the movzx instruction jmovzx allows you to move a smaller operand into a larger one jand zeros out the upper part of the larger operand ; copy bl to dl and zero ; out upper part of edx ; add edx to eax WriteHex ; Print number in eax to the console in hex ; add ; add ax, bl eax, bx . ; illegal. both operands must be same size ; illegal. both operands must be same size , effffh ; Problem if sum will not fit into destination ;bx = fffffh. ffffh is the largest 2 byte number 1ch ;dx = 1ch jerror: sum will not fit into dx (FFFF+1C = 1991Bh but edx contains only Ibh) ; solution if sum will not fit into destination: copy both operands to a larger type using movzx ;then do addition 83 84 0+ 85 86 87 - Ich ;reinitialize dx to Ich ; copy dx to cx and zero out the upper part of ecx ; copy bx to si and zero out the upper part of esi ; add esi to ecx: ffffh+ICH =1891bh jadd 2 variables , offset bNum: get address of bum to look at bNum in memory window vadd anum, bNum; illegal. no memory to memory operations 89 90 20 91 92 22 93 94. 27 95 96 20 97 98 30 99 199 100 191 101 192 102 103 104 105 106 ito add one variable to another first copy one of them to a register , aNum bNum, ; copy anum to a register ; add al to bum and store answer back in bum Readchar ExitProcess, ; Pause program execution while user inputs a non-displayed char jexit to dos: Like C++ exit() INVOKE main ENDP END main

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago