16
CSCI2510 Computer Organization Tutorial 02: MASM Basic Structs and Operations Bentian Jiang [email protected]

CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

  • Upload
    others

  • View
    21

  • Download
    2

Embed Size (px)

Citation preview

Page 1: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

CSCI2510 Computer Organization

Tutorial 02: MASM Basic

Structs and Operations

Bentian Jiang

[email protected]

Page 2: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Review

• You can use the video demo for tuto 1 to revisit how

to setup basic MASM environment and how to see

register windows step by step:

– http://www.cse.cuhk.edu.hk/~mcyang/csci2510.html

• Please create a new file for each program. If you

want to have two assembly file in one project, please

exclude all assembly file until only one included, and

rebuild it.

– [Right Click] the .asm file > [Exclude From Project].

– You can include it again in [Project] > [Show All Files]

CSCI2510 Tut02: MASM Basic Structs and Operations 2

Page 3: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Program structure

CSCI2510 Tut02: MASM Basic Structs and Operations 3

Page 4: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Assembler Directives

• Telling the assembler what to do:

– Option, configuration, syntax etc…

• .386

– Use 80386 instruction set (intel 1985’s architecture, most

common supported)

• .model flat

– Memory model of the assembly program

– Only flat model is supported under Win32 program

• (.model) stdcall

– Function calling convention, parameter passed from right to

left (stack)

CSCI2510 Tut02: MASM Basic Structs and Operations 4

Page 5: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Includes Files

• casemap:none

– The assembly is case insensitive

– i.e. Label = label = IAbEl

• include windows.inc

• include kerner32.inc

– Include the files, which handles the system calls

– E.g. invoke ExitProcess, 0 (valid after include files)

• include user32.inc

– Graphical User Interface (GUI) elements in windows

CSCI2510 Tut02: MASM Basic Structs and Operations 5

Page 6: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Data Segment & Datatypes

• “.data” is also assembly directives

– Declare and apply some memory space in primary memory

(e.g. RAM)

• Datatypes: integer DB, DW, DWORD, DQ

– SINGLEBYTE DB, 8-bits

– TWOBYTE DW, 16-bits

– FOURBYTE DWORD, 32-bits

– EIGHTBYTE DQ, 64-bits

CSCI2510 Tut02: MASM Basic Structs and Operations 6

Page 7: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Data Segment & Datatypes

CSCI2510 Tut02: MASM Basic Structs and Operations 7

Page 8: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Code Segment

• “.code” is also assembly directives

– State the following segment is the program assembly code

• start:

– Label that indicates where should the program begins

– End function with “end start”

• Comment in masm: “;”

– It will directly comment the whole line

CSCI2510 Tut02: MASM Basic Structs and Operations 8

Page 9: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

I/O (Console)

• C style function to input/output information

– crt_printf: print like a C program (like printf() in C)

– crt_scanf: read the input (like scanf() in C)

– console application

• Example:

CSCI2510 Tut02: MASM Basic Structs and Operations 9

Page 10: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

I/O (Console)

• Download the new library:

– In Blackboard -> CSCI2510 -> course content -> msvcrt.zip

• Open VS community, click [New] > [Project] > [Visual

C++] > [Windows Desktop] > [Windows Desktop

Wizard] > [Console application] > only choose [Empty

project].

CSCI2510 Tut02: MASM Basic Structs and Operations 10

Page 11: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

I/O (Console)

• Configure the project setting like the previous program

– Left the Subsystem be [Console] instead of [Windows]

• In the [Project] > [Property], add the newly downloaded library

to [Linker] > [General] > [Additional Library Directories]

– e.g. D:\csci2510\lib

CSCI2510 Tut02: MASM Basic Structs and Operations 11

Page 12: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Reference for Programming ex1.386

.model flat, stdcall

option casemap:none

include windows.inc

include kernel32.inc

include msvcrt.inc

includelib msvcrt.lib

.data

APrompt db "CSCI2510 Tutorial 2", 10, 0

.code

start:

invoke crt_printf, addr APrompt

invoke ExitProcess, NULL

end start

; press Ctrl + F5 to prevent instant quit after program execution

CSCI2510 Tut02: MASM Basic Structs and Operations 12

Page 13: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Hints for HW1

• For question 1, you can find corresponding concepts

in lecture 01, please answer the questions in your

own words.

• For question 2, you can find corresponding concepts

in lecture 02.

• For question 3, you can find corresponding concepts

in lecture 03.

CSCI2510 Tut02: MASM Basic Structs and Operations 13

Page 14: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Extended ASCII for HW1 Q2

CSCI2510 Tut02: MASM Basic Structs and Operations 14

Page 15: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Hint for Q2

• Example to translate a 16-bits word into string of

characters:

– E641h, find the corresponding hex in extended ASCII table

• E6h 230 E6h µ

• 41h 65 41h A

• So the result is “µA”.

• You can find how to interpret a word in other methods

in slide of lecture 02.

CSCI2510 Tut02: MASM Basic Structs and Operations 15

Page 16: CSCI2510 Computer Organization Tutorial 02: MASM Basic ...mcyang/csci2510/2018F/Tut02... · CSCI2510 Tut02: MASM Basic Structs and Operations 12. Hints for HW1 • For question 1,

Summary

• Reference for programming exercise 1

• Hints for HW1 Q1-Q3

CSCI2510 Tut02: MASM Basic Structs and Operations 16