24
CS2422 Assembly Language & System Programming November 21, 2006 Todays Topics (1/2) Assignment 3 Windows (graphics mode) programming. Linking to an assembly procedure. Setting up Visual C++ 2005 Express.

CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

Embed Size (px)

Citation preview

Page 1: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

1

CS2422 Assembly Language & System Programming

November 21, 2006

Today�s Topics (1/2)

� Assignment 3� Windows (graphics mode) programming.� Linking to an assembly procedure.� Setting up Visual C++ 2005 Express.

Page 2: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

2

References

� Setting up Platform SDK: see the course webpage.

� Setting up Visual C++ 6.0 to compile and link assembly code with C or C++ code: included in the download file

� theForger�s Win32 API Tutorial http://winprog.org/tutorial. (Read at least the first two articles.)

Demo

� The program creates a �mosaic� effect on your windows desktop.

� You need to set your display to use 24-bit or 32-bit colors.

� Using ML.exe /Zi option to enable debugging directly within your assembly code at Visual C++. (Already done for you if you use VC++ 2005 Express.)

Page 3: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

3

Assembly Code to Be Written

Mosaic(img, resX, resY, x, y, blockX, blockY)

Example Code

� Mosaic_C_version() in main.c shows the algorithm.

� To run the assembly code, remember to comment out:#define MOSAIC_C_VERSION

� The Mosaic procedure in mosaic.asmgives an example of accessing parameters and local variables.

Page 4: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

4

Img: a 2D Array� The img[] is conceptually 2D, but stored as if it

is a 1D array.� A pixel at location (x, y) is stored at img[x + y * xRes]

� Each pixel stores the Red, Green, Blue components of a color in 4 bytes:

struct {BYTE rgbBlue;BYTE rgbGreen;BYTE rgbRed;BYTE rgbReserved;

} RGBQUAD;

Basic Structure of a Windows Program

� WinMain procedure.� WinProc procedure.� Section 11.2 explains the above in assembly.

The C version in main.c might be easier to read.

� An excellent introductory material can be found at: http://winprog.org/tutorial. (Read at least the first two articles.)

Page 5: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

5

WinMain ProcedureEvery Windows application needs a startup procedure, usually named WinMain, which is responsible for the following tasks:� Get a handle to the current program� Load the program�s icon and mouse cursor� Register the program�s main window class and identify the

procedure that will process event messages for the window

� Create the main window

� Show and update the main window� Begin a loop that receives and dispatches messages

WinProc Procedure� WinProc receives and processes all event messages

relating to a window� Some events are initiated by clicking and dragging the

mouse, pressing keyboard keys, and so on � WinProc decodes each message, carries out

application-oriented tasks related to the messageWinProc PROC,

hWnd:DWORD, ; handle to the windowlocalMsg:DWORD, ; message IDwParam:DWORD, ; parameter 1 (varies)lParam:DWORD ; parameter 2 (varies)

(Contents of wParam and lParam vary, depending on the message.)

Page 6: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

6

What Will You Learn from This Assignment?

� Linking C and Assembly.� The first step in Windows programming.� Array processing.� Implementation of nontrivial loops.

Today�s Topic (2/2)

� Chapter 1 of Beck�s �System Software�book.

Page 7: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

7

Study Guide

� 1.1 Introduction� 1.2 System Software and Machine Architecture� 1.3 The Simplified Instructional Computer (SIC)

� SIC Machine Architecture� SIC/XE Machine Architecture� SIC Programming Examples

Introduction

� Definition of System software� System software consists of a variety of

programs that support the operation of a computer

� E.g. of system software� Text editor, compiler, loader or linker,

debugger, macro processors, operating system, database management systems, software engineering tools, �.

Page 8: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

8

System Software and Machine Architecture (1/2)

� One characteristic in which most system software differs from application software is machine dependency

� System programs are intended to support the operation and use of the computer itself, rather than any particular application.

System Software and Machine Architecture (2/2)

� Because most system software is machine-dependent, we must include real machines and real pieces of software in our study.

� Simplified Instructional Computer (SIC)� SIC is a hypothetical computer that has been

carefully designed to include the hardware features most often found on real machines, while avoiding unusual or irrelevant complexities

Page 9: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

9

The Simplified Instructional Computer (SIC)

� Like many other products, SIC comes in two versions� The standard model � An XE version

� �extra equipments�, �extra expensive�

� The two versions has been designed to be upward compatible

SIC Machine Architecture (1/7)

� Memory� Memory consists of 8-bit bytes� Any 3 consecutive bytes form a word (24 bits)� Total of 32768 (215) bytes in the computer

memory

Page 10: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

10

SIC Machine Architecture (2/7)� Registers

� Five registers� Each register is 24 bits in length

Status word9SWProgram counter8PCLinkage register2LIndex register1X

Accumulator0ASpecial useNumberMnemonic

SIC Machine Architecture (3/7)

� Data Formats� Integers are stored as 24-bit binary number� 2�s complement representation for negative

values� Characters are stored using 8-bit ASCII codes� No floating-point hardware on the standard

version of SIC

Page 11: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

11

SIC Machine Architecture (4/7)

� Instruction Formats� Standard version of SIC

addressxopcode1518

The flag bit x is used to indicate indexed-addressing mode

SIC Machine Architecture (5/7)

� Addressing Modes� There are two addressing modes available

� Indicated by x bit in the instruction

TA=address+(X)x=1Indexed

TA=addressx=0Direct

Target address calculationIndicationMode

(X): the contents of register X

Page 12: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

12

SIC Machine Architecture (6/7)� Instruction Set

� Load and store registers� LDA, LDX, STA, STX, etc.

� Integer arithmetic operations� ADD, SUB, MUL, DIV� All arithmetic operations involve register A and a word in

memory, with the result being left in A

� COMP� Conditional jump instructions

� JLT, JEQ, JGT

� Subroutine linkage� JSUB, RSUB

� See appendix A, Page 495

SIC Machine Architecture (7/7)

� Input and Output� Input and output are performed by transferring

1 byte at a time to or from the rightmost 8 bits of register A

� Test Device TD instruction� Read Data (RD)� Write Data (WD)

Page 13: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

13

SIC Programming Examples (Fig 1.2a)

LDA FIVESTA ALPHALDCH CHARZSTCH C1...

ALPHA RESW 1 one-word variableFIVE WORD 5 one-word constantCHARZ BYTE C�Z� one-byte constantC1 RESB 1 one-byte variable

SIC Programming Example (Fig 1.3a)LDA ALPHAADD INCRSUB ONESTA BETALDA GAMMAADD INCRSUB ONESTA DELTA......

ONE WORD 1 one-word constantALPHA RESW 1 one-word variablesBETA RESW 1GAMMA RESW 1DELTA RESW 1INCR RESW 1

Page 14: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

14

SIC Programming Example (Fig 1.4a)

LDX ZERO initialize index register to 0MOVECH LDCH STR1,X load char from STR1 to reg A

STCH STR2,XTIX ELEVEN add 1 to index, compare to 11JLT MOVECH loop if �less than�...

STR1 BYTE C�TEST STRING�STR2 RESB 11ZERO WORD 0ELEVEN WORD 11

SIC Programming Example (Fig 1.5a)LDA ZERO initialize index value to 0STA INDEX

ADDLP LDX INDEX load index value to reg XLDA ALPHA,X load word from ALPHA into reg AADD BETA,XSTA GAMMA,X store the result in a word in GAMMALDA INDEXADD THREE add 3 to index valueSTA INDEXCOMP K300 compare new index value to 300JLT ADDLP loop if less than 300......

INDEX RESW 1ALPHA RESW 100 array variables�100 words eachBETA RESW 100GAMMA RESW 100ZERO WORD 0 one-word constantsTHREE WORD 3K300 WORD 300

Page 15: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

15

SIC Programming Example (Fig 1.6)INLOOP TD INDEV test input device

JEQ INLOOP loop until device is readyRD INDEV read one byte into register ASTCH DATA..

OUTLP TD OUTDEV test output deviceJEQ OUTLP loop until device is readyLDCH DATAWD OUTDEV write one byte to output device..

INDEV BYTE X�F1� input device numberOUTDEV BYTE X�05� output device numberDATA RESB 1

SIC/XE Machine Architecture (1/13)

� Memory� Maximum memory available on a SIC/XE

system is 1 megabyte (220 bytes)

Page 16: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

16

SIC/XE Machine Architecture (2/13)

� Registers� Additional registers are provided by SIC/XE

Floating-point accumulator (48 bits)6FGeneral working register5TGeneral working register4S

Base register3BSpecial useNumberMnemonic

SIC/XE Machine Architecture (3/13)

� There is a 48-bit floating-point data type

fractionexponents36111

F*2(e-1024)

Page 17: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

17

SIC/XE Machine Architecture (4/13)� Instruction Formats

op8

r2r1op448

Format 1 (1 byte)

Format 2 (2 bytes)

Formats 1 and 2 are instructions that do not reference memory at all

x1

b1

p1

n1

i1

dispeop1216

Format 3 (3 bytes)

x1

b1

p1

n1

i1

addresseop2016

Format 4 (4 bytes)

SIC/XE Machine Architecture (5/13)

� Addressing modes� Base relative (n=1, i=1, b=1, p=0)� Program-counter relative (n=1, i=1, b=0, p=1)� Direct (n=1, i=1, b=0, p=0)� Immediate (n=0, i=1, x=0)� Indirect (n=1, i=0, x=0)� Indexing (both n & i = 0 or 1, x=1)� Extended (e=1)

Page 18: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

18

SIC/XE Machine Architecture (6/13)� Base Relative Addressing Mode

x1b

0p

1n

1i

dispopcodee

n=1, i=1, b=1, p=0, TA=(B)+disp (0≤disp ≤4095)

� Program-Counter Relative Addressing Mode

x0b

1p

1n

1i

dispopcodee

n=1, i=1, b=0, p=1, TA=(PC)+disp (-2048≤disp ≤2047)

SIC/XE Machine Architecture (7/13)� Direct Addressing Mode

x0b

0p

1n

1i

dispopcodee

n=1, i=1, b=0, p=0, TA=disp (0≤disp ≤4095)

1x

0b

0p

1n

1i

dispopcodee

n=1, i=1, b=0, p=0, TA=(X)+disp (with index addressing mode)

Page 19: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

19

SIC/XE Machine Architecture (8/13)� Immediate Addressing Mode

0x b p

0n

1i

dispopcodee

n=0, i=1, x=0, operand=disp

� Indirect Addressing Mode

0x b p

1n

0i

dispopcodee

n=1, i=0, x=0, TA=(disp)

SIC/XE Machine Architecture (9/13)� Simple Addressing Mode

x b p0n

0i

dispopcodee

i=0, n=0, TA=bpe+disp (SIC standard)

x b p1n

1i

dispopcodee

i=1, n=1, TA=disp (SIC/XE standard)

Page 20: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

20

SIC/XE Machine Architecture (10/13)� Addressing Modes Summary (p.499)

SIC/XE Machine Architecture (11/13)� Instruction Format

Page 21: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

21

SIC/XE Machine Architecture (12/13)

� Instruction Set� Instructions to load and store the new registers

� LDB, STB, etc.� Floating-point arithmetic operations

� ADDF, SUBF, MULF, DIVF� Register move instruction

� RMO� Register-to-register arithmetic operations

� ADDR, SUBR, MULR, DIVR� Supervisor call instruction

� SVC

SIC/XE Machine Architecture (13/13)

� Input and Output� There are I/O channels that can be used to

perform input and output while the CPU is executing other instructions

Page 22: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

22

SIC/XE Programming Examples (Fig 1.2b)

LDA #5STA ALPHALDCH #90STCH C1...

ALPHA RESW 1C1 RESB 1

LDA FIVESTA ALPHALDCH CHARZSTCH C1...

ALPHA RESW 1FIVE WORD 5CHARZ BYTE C�Z�C1 RESB 1

SIC version SIC/XE version

SIC/XE Programming Example (Fig 1.3b)LDS INCRLDA ALPHAADDR S,ASUB #1STA BETALDA GAMMAADDR S,ASUB #1STA DELTA......

ALPHA RESW 1 one-word variablesBETA RESW 1GAMMA RESW 1DELTA RESW 1INCR RESW 1

Page 23: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

23

SIC/XE Programming Example (Fig 1.4b)

LDT #11 initialize register T to 11LDX #0 initialize index register to 0

MOVECH LDCH STR1,X load char from STR1 to reg ASTCH STR2,X store char into STR2TIXR T add 1 to index, compare to 11JLT MOVECH loop if �less than� 11...

STR1 BYTE C�TEST STRING�STR2 RESB 11

SIC/XE Programming Example (Fig 1.5b)

LDS #3LDT #300LDX #0

ADDLP LDA ALPHA,X load from ALPHA to reg AADD BETA,XSTA GAMMA,X store in a word in GAMMAADDR S,X add 3 to index valueCOMPR X,T compare to 300JLT ADDLP loop if less than 300......

ALPHA RESW 100 array variables�100 words eachBETA RESW 100GAMMA RESW 100

Page 24: CS2422 Assembly Language & System Programmingchunfa/cs2422/slides/Week11a.pdfŁ Instruction Set Œ Instructions to load and store the new registers Ł LDB, STB, etc. Œ Floating-point

24

SIC/XE Programming Example (Fig 1.7b)