Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Lab 1 - GETTING STARTED

Objectives
This lab introduces you how to install and use Visual studio 2019 for programming and debugging an
assembly language program. Building and running a 32-bit program a 64-bit program

Lab Work

Download Visual Studio


To download the Visual Studio installer file, choose the following button, choose the Community edition
of Visual Studio (VS) 2019 from the following link, choose Save, and then choose Open folder.

Run the VS installer, select the Desktop development with C++ button in the installer window, look at
the Summary list on the right side to verify that VC++ is selected, and click the Modify button in the
lower right corner of the window, as below in Figure

The Visual C++ language includes the Microsoft Assembler (MASM). To verify that MASM is installed,
open a Windows Explorer window and look for the file named ml.exe in the Visual Studio installation
directory, such as C:\Program Files (x86)\Microsoft Visual
Studio\2019\Community\VC\Tools\MSVC\14.xx.xxxx\bin\HostX64\x86. (The "x" characters above
indicate digits in the version number of your current VS installation.)
Setting up Visual Studio and Building and running a 32-bit program

Follow these steps:

1. Start Visual Studio.


2. Select C++ language, as shown below

3. Select Console App

4. Then write the Project name, without spaces, and make sure you select Place solution and
project in the same directory, and finally create.
5. Once the project has been created, you will see the project name in Visual Studio's Solution
Explorer window. You should also see a C++ source file in the project named Lab1.cpp. Double-
click the file name to open it in the editor.
Remove Lab1.cpp by right click on the file and delete it

6. Add a new .asm file to the project, right click on the Source Files folder and select from the
menu Add, and add New Item.
7. Once the project has been opened, you will see the project name in Visual Studio's Solution
Explorer window. You should also see an assembly language source file in the project named
Task1.asm. Double-click the file name to open it in the editor.

The asm file Task1.asm is empty file; copy the code below into the Task1.asm and save it.:

; AddTwo.asm - adds two 32-bit integers.


; Chapter 3 example
.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.code
main proc
mov eax,5
add eax,6

invoke ExitProcess,0
main endp
end main
8. Right click on the Project, and select form the menu Build Dependences, then Build
Customizations.

9. A new window will open, select masm from the available build customizations files, and then
OK.
10. Return back to the Task1.asm, right click on Task1.asm file, select properties, from the General
tab, select No for to Excluded From Build, and select Microsoft Macro Assembler for the Item
Type

11. Right click on Lab1 Project, select properties, from linker options select Advanced, then specify
the Entry point to main
Build the Program

Now you will build (assemble and link) the sample program. Select Build Project from the Build menu. In
the Output window for Visual Studio at the bottom of the screen, you should see messages similar to the
following, indicating the build progress:

Run the Program in Debug Mode

First, you must set a breakpoint. When you set a breakpoint in a program, you can use the debugger to
execute the program a full speed (more or less) until it reaches the breakpoint. At that point, the
debugger drops into single-step mode. Here's how to do it:

1. Make sure the ASM source code file is open in the editor window.
2. Right click the mouse along the border to the left of the mov eax,5 statement, select breakpoint,
then insert breakpoint, A large red dot should appear in the margin.

3. Select Start Debugging from the Debug menu. The program should run and pause on the line
with the breakpoint. (Optionally, you can close the Diagnostic Tools, Autos, and Call Stack
windows.)
4. Press the F10 key (called Step Over) to execute the current statement. Continue pressing F10
until the program is about to execute the invoke statement.
5. A small black window icon should appear on either your Windows desktop or status bar. The
window should be blank because this program does not display any output.
6. Press F10 one more time to end the program.
You can remove a breakpoint by right clicking its dot then delete the breakpoint. Take a few minutes to
experiment with the Debug menu commands. Set more breakpoints and run the program again.

Examine the registers 

The Registers window displays register contents during Visual Studio debugging.


During debugging, register values change as code executes in your app. Values that have changed
recently appear in red in the Registers window.
To open the Registers window

To view the registers content, While debugging is running or at a breakpoint, select


Debug > Windows > Registers, or press Alt+5.
Lab Work
1. Watch the values of program variables

Debug the program

 Right-clicking on a line of code to set a breakpoints:


o Add three breakpoints at the code lines

mov eax,5
add eax,6
invoke ExitProcess,0

(a red circle will be set at the beginning of each line)


o Click start debugging ( or click F5)
o In Debug mode, you can notice when the program start running and it will pause at the first
breakpoint ( a yellow arrow will appear at the beginning of the line were we set the
breakpoint)
Navigating the registers, what is the value of the EAX register _________
o Continue the run to the next breakpoint, what is the value of the EAX register __________
o Continue the run to the last breakpoint, what is the value of the EAX register __________
o Notice that the register with red color are the registers that changes its value.
o Mention the registers that changed its value other than EAX_________, and why?

2. Run the same code in 64-bit Debug mode, select x64 and Build your project, list the error you
see and explain what they means :_________________________________________________

Differences between a 32-bit program and 64-bit program


Let’s use Task1.asm program shown earlier, and modify it for 64-bit programming. We will use the 64-bit register
RAX to accumulate two integers, and store their sum in a 64-bit variable:
; AddTwoSum_64.asm - Chapter 3 example.
ExitProcess proto

.data
sum qword 0

.code
main proc
mov rax,5
add rax,6
mov sum,rax

mov ecx,0
call ExitProcess

main endp
end

This program is different from the 32-bit version we showed earlier:

 The following three lines, which were in the 32-bit version of the Task1.asm program are not
used in the 64-bit version:
.386
.model flat,stdcall
.stack 4096

 Statements using the PROTO keyword do not have parameters in 64-bit programs. :
ExitProcess PROTO

This was our earlier 32-bit version:


ExitProcess PROTO,dwExitCode:DWORD

 Lines 14–15 use two instructions to end the program (mov and call). The 32-bit version used an
INVOKE statement to do the same thing. The 64-bit version of MASM does not support the
INVOKE directive.
 In line 17 (end), the end directive does not specify a program entry point. The 32-bit version of
the program did.

Using 64-Bit Registers


You can use the integers that are larger than 32 bits by using 64-bit registers and variables. For example,
this is how we could make our sample program use 64-bit values:

• We would change DWORD to QWORD when declaring the sum variable.

• We would change EAX to its 64-bit version, named RAX.

Building and running a 64-bit program


In this section you will assemble, link, and run a sample 64-bit program

Do the following steps, in order:

Right click on Lab1 Project, select properties, from linker options select Advanced, and then specify the
Entry point to main
You should see the following program in the editor window:

Build the Program


Select Build Project from the Build menu. You should see text written to Visual Studio's output
window like the following:
Observe variables with a Watch window
You can open more than one Watch window, and observe more than one variable in  Watch window.
Open a Watch window by selecting  Debug > Windows > Watch > Watch 1, or pressing Ctrl+Alt+W > 1.
You can open additional Watch windows by selecting windows 2, 3, or 4. In the Watch window, select an
empty row, and type variable name, in our program the variable name is sum.

Insert the variable name (sum), and press enter. Run the program and observe the value of the variable,
after you finish the run, what is the new values of the sum variable_________

Memory window
The Memory windows are available only during a debugging session.
To open a Memory window

1. Make sure Enable address-level debugging is selected in Tools > Options (or Debug > Options)


> Debugging > General.> Debugging > General.
2. Start debugging by selecting the green arrow, pressing F5, or selecting Debug > Start Debugging.
3. Under Debug > Windows > Memory, select Memory 1, Memory 2, Memory 3, or Memory 4.

You might also like