machine language - 3f03 alan wassyng itb 166 [email protected] january - april 2006

218
Machine Language - 3F03 Alan Wassyng ITB 166 [email protected] January - April 2006

Upload: daniella-warner

Post on 28-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

Machine Language - 3F03

Alan Wassyng

ITB 166

[email protected]

January - April 2006

Page 2: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

2

3F03 - Overview

• Before we start, let’s refresh our knowledge of C. Consider how we could solve the following problem in C: We have an analog I/O hardware module

that provides 12-bit analog input and output capability for 8 inputs and 8 outputs on a PC.

Construct a C routine to read any one of the analog inputs - 0..7

int GetAI(int IOpoint);

Page 3: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

3

3F03 - Overview

• How the analog board works Set the IO channel using the IOpoint Set the control register according to the IO

channel LSB Read the value at the data register - that

value is the LSB of the analog input Set the control register according to the IO

channel MSB Read the value at the data register - that

value is the MSB of the analog input

Page 4: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

4

3F03 - Overview

• Definitions Base address = 0100h Control register address = Base address+1

Data register address = Base address LSB of IO channel = IOpoint*2 MSB of IO channel = IOpoint*2+1

Page 5: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

5

3F03 - Overview

• Required StepsSo, create OutPort(p, val) that sets the value of hardware port p, to val, and create InPort(p) that reads the value of port p. Then the following sequence reads the analog input value of IOpoint (IOpoint = 0,1,2,3,4,5,6,7. p and val are 16-bit).

OutPort(Control register, LSB of IO channel)

ValueLSB = InPort(Data register)

OutPort(Control register, MSB of IO channel)

ValueMSB = InPort(Data register)

Value = ValueMSB*256+ValueLSB

Page 6: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

6

3F03 - Overview

• You have 10 minutes to rough-out a solution, i.e C code for OutPort, InPort and GetAI. Do it in groups of 3 or 4.

Page 7: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

7

int GetAI(int IOpoint);

void OutPort(int p, int val);

int InPort(int p);

OutPort(Control register, LSB of IO channel)

ValueLSB = InPort(Data register)

OutPort(Control register, MSB of IO channel)

ValueMSB = InPort(Data register)

Value = ValueMSB*256+ValueLSB

• Base address = 0100h

• Control register address = Base address+1 • Data register address = Base address• LSB of IO channel = IOpoint*2• MSB of IO channel = IOpoint*2+1

3F03 - Overview

least significant byte

Page 8: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

8

3F03 - Overview

• So, what did you find? It is not obvious as to how to access the

hardware from within a standard C program.

• High-level languages were developed to enable programmers to work at a higher level of abstraction without concerning themselves too much about the hardware - but it is sometimes necessary to “sink” to the hardware level.

Page 9: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

9

3F03 - Overview

• Course Rules TAs

• Yazhi Wang - [email protected]• Ryan Lortie - [email protected]

Assignments• 4 assignments - 16% of total grade*• Exams - open book• Mid Term - 30% of total grade*• Final Exam - 54% of total grade*

* assuming a final exam grade of 50%. If the final exam grade is < 50% it will be used as the total grade for the course.

Page 10: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

10

3F03 - Overview

• Course Rules Lectures Tue,Wed,Fri 9:30 ABB 271 Tutorial Thur 9:30 ITB 237

http://www.cas.mcmaster.ca/~se3f03/

Page 11: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

11

3F03 - Overview

• Course Rules Assignments & Exams

• Do - explain what you have done and what you have assumed

• Do - keep to the topic• Do - support your arguments with direct

reference to code extracts where necessary• Don’t - assume the reader knows more than

you do• Don’t - pad your answers with incorrect facts!• SHORT IS BETTER THAN WRONG

Page 12: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

12

3F03 - Overview

• Course Objectives Machine level programming concepts. The role of assembler programming in the

current software industry. How the concepts of assembler

programming can help you even though you, personally, program all the time in a high-level language.

Page 13: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

13

3F03 - Overview

Table of ContentsAssembler basicsComputer arithmetic, binary, octal, hex80x86 assembler through example

problems - Debug & NASMBIOS and Operating SystemsSimple data structures in assemblerUnderstanding compiler outputInline assemblerInterfacing to high-level languagesMIPS - Using SPIMCISC versus RISC hardware

132022

75112146190201204216

Page

Page 14: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

14

3F03 - Assembler Basics

• Machine Language A typical processor

RegistersArithmetic & Logic

Control

Data Path

Registers are simply fast memory locations. Some may have specific roles to play, while others may be more general.

Page 15: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

15

3F03 - Assembler Basics

• Word size The word size of a processor is the largest

number of bits it works with as a unit. The original Intel 8088/8086 has a 16-bit

word size. The registers are 16 bits and the 8086 works with 16 bit words in memory. The 8088 has an 8-bit data bus.

The Intel Pentium chips have a 32-bit word size.

Later we’ll see how Intel retains backward compatibility in its processors.

Page 16: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

16

3F03 - Assembler Basics

• Machine Instructions Each processor has its own unique set of

registers and instructions. Examples:

10110100 00001001 moves 09 into highbyte of reg AX

01000011 increment reg BXby 1

Note the difference in these instructions. The first one required 2 bytes, the second only 1 byte. (They also require a different number of clock cycles.)

Page 17: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

17

3F03 - Assembler Basics

• Assembler Converts easier to read/create mnemonics

and operands to machine language. One-to-one correspondence between

assembler statements and machine instructions - usually, since sometimes an assembler statement is an instruction to the assembler program itself.

Assemblers are often augmented by macro extensions.

Page 18: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

18

3F03 - Assembler Basics

• 80x86 assembler (Intel notation) General structure

label mnemonic operand(s) comment

SET: MOV AX,BX ;move BX into AX dest,source

ADD AX,BX ;add BX to AX and

;store in AX

Page 19: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

19

3F03 - Assembler Basics

• 80x86 Layout

8 bits16 bits32 bits

EAX

EBX

ECX

EDX

ESIEDIESPEBP

AH AL

BH BL

CH

DH DL

CL

CSDSSSES

FSGS

Page 20: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

20

3F03 - Assembler Basics

• 80x86 “Special” Registers IP - The instruction pointer

• points to next instruction to be exceuted

FLAGS (EFLAGS in 80386 and higher)• OF - overflow• DF - direction (strings in memory)• IF - interrupt• TF - trap• SF - sign• ZF - zero• AF - auxiliary carry (for BCD arithmetic)• PF - parity• CF - carry

Page 21: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

21

3F03 - Computer arithmetic, binary, octal, hex

• Integers Be specific about size (number of bits) and

signage (signed or unsigned). Typical 16-bit signed integers:

• Stored in 2’s complement format• Bit 15 (msb): 1 negative, 0 positive• -32768 integer 32767• 1000000000000000 i 0111111111111111• 8000 i 7FFF

Unsigned integers• 0 integer 65535• 0 integer FFFF

Page 22: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

22

3F03 - Computer arithmetic, binary, octal, hex

• Octal DEC used octal for its very successful mini-

computers - PDP and VAX. Not used much in current architectures.

• Hex Almost everything we do in the assembler

will be in Hex. Get familiar with it! Each hex digit represents a nybble

0 - F represents 0000 - 1111

Page 23: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

23

3F03 - 80x86 assembler

• Addresses Addresses in the 8088, 8086, 80286 world

are written in the form• segment:offset• segments start on 16 byte boundaries - the first

at 0000, the second at 0010, etc. We specify the number of the segment, 0000, 0001 etc.

• offset is a 16 bit address that represents an offset from the start of the specified segment.

Addresses in the 80386 (and later) world can mimic the earlier, restricted addresses, or can be represented by a “flat” model so that segments are not necessary.

Page 24: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

24

3F03 - 80x86 assembler

• Addresses Consider the address 0001:0032

0000

0001

0002

Segment 0

Segment 1

Segment 2

0001:0032 = 0000:0042 = 0002:0022

Page 25: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

25

3F03 - 80x86 assembler

• Open a DOS window and run Debug.

Page 26: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

26

3F03 - 80x86 assembler

• A few Debug commands-a assemble following statements

end by blank statement

-u unassemble

-u addr unassemble from addr(ess)

-t trace, execute next statement

and show registers

-r show registers

-? get list of commands

-q quit

Page 27: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

27

3F03 - 80x86 assembler

• Debug examples

Page 28: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

28

3F03 - 80x86 assembler

ADD - Arithmetic Addition

Usage: ADD dest,src Modifies flags: AF CF OF PF SF ZF

Adds "src" to "dest" and replacing the original contents of "dest". Both operands are binary.

Clocks SizeOperands 808x 286 386 486 Bytes

reg,reg 3 2 2 1 2mem,reg 16+EA 7 7 3 2-4 (W88=24+EA)reg,mem 9+EA 7 6 2 2-4 (W88=13+EA)reg,immed 4 3 2 1 3-4mem,immed 17+EA 7 7 3 3-6 (W88=23+EA)accum,immed 4 3 2 1 2-3

Page 29: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

29

3F03 - 80x86 assembler

INC - Increment

Usage: INC dest

Modifies flags: AF OF PF SF ZF

Adds one to destination unsigned binary operand.

Clocks Size

Operands 808x 286 386 486 Bytes

reg8 3 2 2 1 2

reg16 3 2 2 1 1

reg32 3 2 2 1 1

mem 15+EA 7 6 3 2-4 (W88=23+EA)

Page 30: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

30

3F03 - 80x86 assembler

What happens if we add 12F2 +220F

------ 3501

What if we work with 8 bits at a time?Will this work? mov al, F2

mov ah, 12mov bl, 0Fmov bh, 22add al, bladd ah, bh

Page 31: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

31

3F03 - 80x86 assembler

ADC - Add With Carry

Usage: ADC dest,src

Modifies flags: AF CF OF SF PF ZF

Sums two binary operands placing the result in the destination. If CF is set, a 1 is added to the destination.

Clocks Size

Operands 808x 286 386 486 Bytes

reg,reg 3 2 2 1 2

mem,reg 16+EA 7 7 3 2-4 (W88=24+EA)

reg,mem 9+EA 7 6 2 2-4 (W88=13+EA)

reg,immed 4 3 2 1 3-4

mem,immed 17+EA 7 7 3 3-6 (W88=23+EA)

accum,immed 4 3 2 1 2-3

Page 32: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

32

3F03 - 80x86 assembler

If we use ADC rather than ADD everything works just fine.

mov al, F2mov ah, 12mov bl, 0Fmov bh, 22add al, bladc ah, bh

Page 33: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

33

3F03 - 80x86 assembler

• URL for Intel 80x86 Instructions

http://www.penguin.cz/~literakl/intel/intel.html

Page 34: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

34

3F03 - 80x86 assembler

• Some registers have special roles The names of the general registers were

chosen to reflect the specific roles they can play:

• AX - accumulator (brief history)

• BX - base register

• CX - counter register

• DX - data register

Page 35: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

35

3F03 - 80x86 assembler

• Loop example - in Debug1478:0100 MOV AX,0

1478:0103 MOV CX,5

1478:0106 INC AX

1478:0107 LOOP 0106

Page 36: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

36

3F03 - 80x86 assembler

• Loop example - in Debug1478:0100 MOV AX,0

1478:0103 MOV CX,5

1478:0106 INC AX

1478:0107 LOOP 0106

counter

Page 37: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

37

3F03 - 80x86 assembler

• LOOP - Decrement CX and Loop if CX Not Zero Usage: LOOP label Modifies Flags: None Decrements CX by 1 and transfers control to

"label" if CX is not Zero. The "label" operand must be within -128 or 127 bytes of the instruction following the loop instruction

Page 38: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

38

3F03 - 80x86 assembler

• Loop example - in AssemblerMOV AX,0

MOV CX,5

LBL: INC AX

LOOP LBL

Page 39: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

39

3F03 - 80x86 assembler

• Loop example - in AssemblerMOV AX,0

MOV CX,5

LBL: INC AX

LOOP LBL

Page 40: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

40

3F03 - 80x86 assembler

• Loop example - source file Create file TestLoop.asm in editor

[BITS 16] ; Set 16 bit code

[ORG 0x0100] ; Set code start

[SECTION .text] ; Code segment

mov ax, 0000

mov cx, 0005

LBL: inc ax

loop LBL

mov ax, $4C00 ; Prepare to exit

int $21 ; Terminate prog

Page 41: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

41

3F03 - 80x86 assembler

• Loop example - assemble source Assemble and link file using NASM

NASM TestLoop.asm -o TestLoop.com

Assemble, link & make listing fileNASM TestLoop.asm -l TestLoop.txt -o TestLoop.com

Page 42: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

42

3F03 - 80x86 assembler

• Loop example - TestLoop.txt 1 [BITS 16] ; Set 16 bit code

2 [ORG 0x0100] ; Set code start

3

4 [SECTION .text] ; Code segment

5

6 00000000 B80000 mov ax, 0000

7 00000003 B90500 mov cx, 0005

8 00000006 40 LBL: inc ax

9 00000007 E2FD loop LBL

10

11 00000009 B8004C mov ax, $4C00 ; Prepare to exit

12 0000000C CD21 int $21 ; Terminate prog

Page 43: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

43

3F03 - 80x86 assembler

• Loop example - TestLoop.txt 1 [BITS 16] ; Set 16 bit code

2 [ORG 0x0100] ; Set code start

3

4 [SECTION .text] ; Code segment

5

6 00000000 B80000 mov ax, 0000

7 00000003 B90500 mov cx, 0005

8 00000006 40 LBL: inc ax

9 00000007 E2FD loop LBL

10

11 00000009 B8004C mov ax, $4C00 ; Prepare to exit

12 0000000C CD21 int $21 ; Terminate prog

How is this number calculated?

Page 44: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

44

3F03 - 80x86 assembler

• Loop, with CMP and Jxx - in Debug1478:0100 MOV AX,0

1478:0103 MOV CX,5

1478:0106 INC AX

1478:0107 DEC CX

1478:0108 CMP CX,0

1478:010B JNZ 0106

Page 45: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

45

3F03 - 80x86 assembler

• CMP - Compare Usage: CMP dest,src Modifies Flags: AF CF OF PF SF ZF Subtracts source from destination and

updates the flags but does not save result. Flags can subsequently be checked for conditions.

Page 46: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

46

3F03 - 80x86 assembler

• JXX - Jump Instructions Table

Mnm Meaning Jump Condition

JA Jump if Above CF=0 & ZF=0

JAE Jump if Above or Equal CF=0

JB Jump if Below CF=1

… … ...

Page 47: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

47

3F03 - 80x86 assembler

• Loop, with CMP and Jxx - in Debug1478:0100 MOV AX,0

1478:0103 MOV CX,5

1478:0106 INC AX

1478:0107 DEC CX

1478:0108 JNZ 0106

Page 48: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

48

3F03 - 80x86 assembler

• Typical addressing modes labelopcode operands

An operand can be a:• register - the value contained in a register• immediate - a constant in the instruction• absolute - a memory location in the instruction• register indirect - a memory location whose

address is in a register• displacement - a memory location offset from an

address in a register• indexed - a memory location whose address is

the sum of values in two registers• memory indirect - the address is in a memory

location whose address is in a register

Page 49: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

49

3F03 - 80x86 assembler

• Typical addressing modes Register and immediate modes we have already

seen

MOV AX,1

MOV BX,AX

register immediate

Page 50: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

50

3F03 - 80x86 assembler

• Typical addressing modes Absolute address mode

MOV AX,[0200]

value stored in memory location DS:0200

Page 51: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

51

3F03 - 80x86 assembler

• Typical addressing modes Register indirect

MOV AX,[BX]

value stored at address contained in DS:BX

Page 52: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

52

3F03 - 80x86 assembler

• Typical addressing modes Displacement

MOV DI,4

MOV AX,[0200+DI]

value stored at DS:0204

Page 53: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

53

3F03 - 80x86 assembler

• Typical addressing modes Indexed

MOV BX,0200

MOV DI,4

MOV AX,[BX+DI]

value stored at DS:0204

Page 54: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

54

3F03 - 80x86 assembler

• Typical addressing modes Memory indirect

MOV DI,0204

MOV BX,[DI]

MOV AX,[BX]

If DS:0204 contains 0256,

then AX will contain

whatever is stored at

DS:0256

Page 55: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

55

3F03 - 80x86 assembler

• Typical addressing modes Memory indirect

MOV DI,0204

MOV BX,[DI]

MOV AX,[BX]

If DS:0204 contains 0256,

then AX will contain

whatever is stored at

DS:0256

Byte addresses in memory

0200 0204

0250 0256

0256

1234

DI

BX

AX = 1234

Page 56: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

56

3F03 - 80x86 assembler

• 80x86 Restrictions MOV cannot move memory to memory

MOV AX,[0200] Use this insteadMOV [0210],AX

MOV cannot move a segment register into another segment register.

MOV cannot move immediate data into a segment register.

Mov cannot move an 8-bit register half into a 16-bit register.

In real mode, only BP, BX, SI and DI can hold an offset for memory data.

Page 57: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

57

3F03 - 80x86 assembler

• Complications arising from memory access instructions Consider NEG [BX]

• [BX] is a reference to data stored at the address DS:BX. How big is that data?

• How would we reference just a byte?• Use a type specifier! In Debug and MASM these

are of the form BYTE PTR, WORD PTR etc. In NASM we simply use BYTE, WORD, etc.

e.g. NEG BYTE [BX]

Page 58: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

58

3F03 - 80x86 assembler

• Text strings in assembler Define the string (or a place holder) in the

data segment - actually we define the address at which the string starts as well as the characters in the string.

Move the starting address of the string into a register, and go from there ….

Page 59: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

59

3F03 - 80x86 assembler

; TextEx.asm

[BITS 16] ; Set 16 bit code

[ORG 0100H] ; Set code start address to 100h (COM file)

[SECTION .text] ; Code Section

START:

mov dx, msg ; Loads ADDRESS of msg into dx

mov ah,9 ; DOS Fn 9 displays text pointed to by dx

; to standard output.

int 21H ; INT 21H - DOS interrupt.

mov ax, 04C00H ; This DOS function exits the program

int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing initialised data

; Now store the text string ending it with CR, LF and "S"

msg db "Hello world", 13, 10, "$"

Page 60: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

60

3F03 - 80x86 assembler

; TextEx2.asm

[BITS 16] ; Set 16 bit code

[ORG 0100H] ; Set code start address to 100h (COM file)

[SECTION .text] ; Code Section

START:

mov dx, msg ; Loads ADDRESS of msg into dx

mov ah,9 ; DOS Fn 9 displays text pointed to by dx

; to standard output.

int 21H ; INT 21H - DOS interrupt.

mov ax, 04C00H ; This DOS function exits the program

int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing initialised data

; Now store the text string ending it with CR, LF and "S"

msg db "Hello world", 0DH, 0AH, "$"

Page 61: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

61

3F03 - 80x86 assembler

; TextEx3.asm

[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionSTART: mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino diCOPY: mov ah, BYTE [si] ; move contents of si into ah mov BYTE [di],ah ; move ah into contents of di inc di ; point to next char in destination inc si ; point to next char in source cmp ah,"$" ; test if end of string jne COPY ; if not end of string, do more chars mov dx,msg ; Loads address of msg into dx mov ah,9 ; DOS Fn 9 displays text pointed to by dx ; to standard output. int 21H ; INT 21H - DOS interrupt. mov dx,msg2 ; Loads address of msg2 into dx mov ah,9 ; DOS Fn 9 int 21H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised data; Now store the text string ending it with CR, LF and "S"msg db "Hello world", 0DH, 0AH, "$"

msg2 times 80 db "$"

Page 62: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

62

3F03 - 80x86 assembler

; TextEx4.asm

[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionSTART: mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino diCOPY: mov ah, BYTE [si] ; move contents of si into ah cmp ah,"a" ; if char < "a" jl NOCHNG ; then jump to NOCHNG cmp ah,"z" ; if char > "z" jg NOCHNG ; then jump to NOCHNG sub ah,"a"-"A" ; if lower case, subtract "a"-"A"NOCHNG: mov BYTE [di],ah ; move ah into contents of di inc di ; point to next char in destination inc si ; point to next char in source cmp ah,"$" ; test if end of string jne COPY ; if not end of string, do more chars mov dx,msg ; Loads address of msg into dx mov ah,9 ; DOS Fn 9 displays text pointed to by dx ; to standard output. int 21H ; INT 21H - DOS interrupt. mov dx,msg2 ; Loads address of msg2 into dx mov ah,9 ; DOS Fn 9 int 21H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised data; Now store the text string ending it with CR, LF and "S"msg db "Hello world", 0DH, 0AH, "$"

msg2 times 80 db "$"

Page 63: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

63

3F03 - 80x86 assembler; TextEx5.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file); Set constantsCR equ 0DHLF equ 0AHEOS equ "$"[SECTION .text] ; Code SectionSTART: mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino diCOPY: mov ah, BYTE [si] ; move contents of si into ah cmp ah,"a" ; if char < "a" jl NOCHNG ; then jump to NOCHNG cmp ah,"z" ; if char > "z" jg NOCHNG ; then jump to NOCHNG sub ah,"a"-"A" ; if lower case, subtract "a"-"A"NOCHNG: mov BYTE [di],ah ; move ah into contents of di inc di ; point to next char in destination inc si ; point to next char in source cmp ah,"$" ; test if end of string jne COPY ; if not end of string, do more chars mov dx,msg ; Loads address of msg into dx mov ah,9 ; DOS Fn 9 displays text pointed to by dx ; to standard output. int 21H ; INT 21H - DOS interrupt. mov dx,msg2 ; Loads address of msg2 into dx mov ah,9 ; DOS Fn 9 int 21H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised data; Now store the text string ending it with CR, LF and "S"msg db "Hello world", CR, LF, EOSmsg2 times 80 db EOS

Page 64: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

64

3F03 - 80x86 assembler

; TextEx6.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file); Set constantsCR equ 0DHLF equ 0AHEOS equ "$"[SECTION .text] ; Code SectionSTART: mov cx,msglen ; move length of msg into cx mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino diCOPY: mov ah, BYTE [si] ; move contents of si into ah cmp ah,"a" ; if char < "a" jl NOCHNG ; then jump to NOCHNG cmp ah,"z" ; if char > "z" jg NOCHNG ; then jump to NOCHNG sub ah,"a"-"A" ; if lower case, subtract "a"-"A"NOCHNG: mov BYTE [di],ah ; move ah into contents of di inc di ; point to next char in destination inc si ; point to next char in source loop COPY ; do more chars until all chars in msg done mov dx,msg ; Loads address of msg into dx mov ah,9 ; DOS Fn 9 displays text pointed to by dx ; to standard output. int 21H ; INT 21H - DOS interrupt. mov dx,msg2 ; Loads address of msg2 into dx mov ah,9 ; DOS Fn 9 int 21H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised data; Now store the text string ending it with CR, LF and "S"msg db "Hello world", CR, LF, EOSmsglen db $-msg

msg2 times 80 db EOS

Page 65: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

65

3F03 - 80x86 assembler

; TextEx6f.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file); Set constantsCR equ 0DHLF equ 0AHEOS equ "$"[SECTION .text] ; Code SectionSTART: mov cx, 0 ; zero cx mov cl,[msglen] ; move length of msg into cl mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino diCOPY: mov ah, BYTE [si] ; move contents of si into ah cmp ah,"a" ; if char < "a" jl NOCHNG ; then jump to NOCHNG cmp ah,"z" ; if char > "z" jg NOCHNG ; then jump to NOCHNG sub ah,"a"-"A" ; if lower case, subtract "a"-"A"NOCHNG: mov BYTE [di],ah ; move ah into contents of di inc di ; point to next char in destination inc si ; point to next char in source loop COPY ; do more chars until all chars in msg done mov dx,msg ; Loads address of msg into dx mov ah,9 ; DOS Fn 9 displays text pointed to by dx ; to standard output. int 21H ; INT 21H - DOS interrupt. mov dx,msg2 ; Loads address of msg2 into dx mov ah,9 ; DOS Fn 9 int 21H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised data; Now store the text string ending it with CR, LF and "S"msg db "Hello world", CR, LF, EOSmsglen db $-msg

msg2 times 80 db EOS

Page 66: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

66

3F03 - 80x86 assembler

• Recap The processor understands binary

instructions (32-bit, 16-bit etc) and works with binary data.

The basic building blocks are registers which are just special memory locations within the processor.

We write programs in Assembler, which is a notation that gets translated (by software) into machine language.

Page 67: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

67

3F03 - 80x86 assembler

• Assembler The principal tasks performed by an

assembler:• Replace opcodes and operands by their

machine level equivalents.• Replace symbolic names by relocatable

addresses.

Page 68: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

68

3F03 - 80x86 assembler

• Linking Why do we need a “linker”? What does it do?

• In general, the assembler produces a “relocatable” object file - a file containing the machine instructions translated from the assembler source code, including “unresolved external symbols”.

• The linker combines all the listed object files, resolves all external references, and constructs an executable file that is ready to be “loaded” and executed.

• The loader has the task of installing the executable file at the correct starting location immediately prior to being invoked.

Page 69: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

69

3F03 - 80x86 assembler

• Relocatable object file - opcodes plus: Import table

• Named locations that are unknown but assumed to be defined in files that will be linked with this one.

Relocation table• Locations within the current file that must be

modified at link time to take into account the offset of the current file within the final exectable program.

Export table• Named locations in the current file that may be

referred to in other relocatable object files.

Page 70: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

70

3F03 - 80x86 assembler

• Complications in linking Relocation typically involves not only

modifying addresses simply by taking into account the offset of the particular file, but may also combine code segments into a single code segment, and data segments into a single data segment, etc.

Page 71: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

71

3F03 - 80x86 assembler

• NASM The Net-wide Assembler is an open source

initiative and is available (free) for a variety of operating systems - DOS, Windows & Linux.

The author(s) tried to make it more explicit and consistent with respect to the way in which it treats memory references than is MASM.

It is capable of producing .COM .OBJ and .EXE files.

Page 72: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

72

3F03 - 80x86 assembler

• NASM NASM is therefore both an assembler and

a linker. In general, we usually use two separate

programs to perform these functions. We can do that now as well. NASM will be used as an assembler, and unless the linking is trivial (a .COM file, or a single source file), we would use a linker such as ALINK to link the required .OBJ files.

Page 73: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

73

3F03 - 80x86 assembler

• Example of an EXE file; TestExe.asmsegment code PUBLIC

Indicate to the linker where execution should start..start:

mov ax,data mov ds,ax Set up data segment mov ax,stack mov ss,ax Set up stack segment mov sp,stacktop Set up stack pointer at top of stack

mov ax,0000 mov cx,0005LBL: inc ax loop LBL

mov ax, $4C00 ; Prepare to exit int 0x21 ; Terminate prog

segment data PUBLIC

segment stack stack resb 64stacktop:

Page 74: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

74

3F03 - 80x86 assembler

NASM TestExe.asm -fobj -o TestExe.obj

Alink TestExe.obj -oEXE -o TestExe.exe

Execute: TestExe

Examine TestExe.exe using Debug

Page 75: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

75

3F03 - 80x86 assembler

; TestExe2.asmsegment code PUBLIC

..start:

mov ax,data mov ds,ax mov ax,stack mov ss,ax mov sp,stacktop

mov cx,0005LBL: mov dx,msg mov ah,09 Display text each time through loop int 0x21 loop LBL

mov ax, $4C00 ; Prepare to exit int 0x21 ; Terminate prog

segment data PUBLICmsg db "This is just an example",CR,LF,EOSCR equ 0x0DLF equ 0x0AEOS equ "$"

segment stack stack resb 64stacktop:

Page 76: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

76

3F03 - 80x86 assembler

; TestExe3.asmsegment code PUBLIC

..start:

mov ax,data mov ds,ax mov ax,stack mov ss,ax mov sp,stacktop

mov cx,0005 mov dx,msg mov ah,09 Safe?LBL: int 0x21 loop LBL

mov ax, $4C00 ; Prepare to exit int 0x21 ; Terminate prog

segment data PUBLICmsg db "This is just an example",CR,LF,EOSCR equ 0x0DLF equ 0x0AEOS equ "$"

segment stack stack resb 64stacktop:

Page 77: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

77

3F03 - 80x86 assembler

• BIOS, OS and Device Drivers Basic Input-Output System provides low

level routines that interface to the hardware through software interrupts.

Typically, the OS includes routines that build on the BIOS level interface to provide users with even more capability than does the BIOS.

Devices that were not included in the BIOS have to be serviced by Device Drivers. These provide input-output services for the particular device.

Page 78: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

78

3F03 - 80x86 assembler

Device Drivers

Hardware

BIOSDD1

DD2

App 1 App 2 App 3

Operating System

VideoKeyboardOther Hardware

Page 79: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

79

3F03 - 80x86 assembler

• Example - Graphics through BIOS Set video modes (INT 10h, Fn 0)

• AH = 0, AL = video mode• Typical video modes

2 80 x 25 2 colours text

3 80 x 25 16 colourstext

0Dh 320 x 200 16 colours graphics 12h 640 x 480 16 colours graphics 13h 320 x 200 256 colours graphics 6Ah 800 x 600 16 colours graphics

• Coordinates 0,0 top left Xmax,Ymax bottom right

Page 80: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

80

3F03 - 80x86 assembler

Get video mode info (INT 10h, Fn 0Fh)• AH = 0Fh• Returns:

AL = current display mode AH = number of columns (characters or pixels) BH = active video page

Page 81: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

81

3F03 - 80x86 assembler

Write graphics pixel (INT 10h, Fn 0Ch)• AH = 0, AL = pixel value (0 to max colours - 1)• BH = video page• CX = x-coordinate• DX = y-coordinate

• If bit 7 in AL is set (=1), the new pixel will be XORed with the current contents of the pixel.

This allows us to erase pixels.

Page 82: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

82

3F03 - 80x86 assembler

• Draw a horizontal line from (100,150) to

(250,150) Set graphics mode For x = 100 to 250

• Set pixel colour• Set pixel

Wait for a key press Exit program

Page 83: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

83

3F03 - 80x86 assembler

; DrawLn1.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionSTART: mov ah,0FH ; Get video info int 10H mov [savemode],al ; Save the current video mode mov ah,0 mov al,0DH int 10H ; switch to graphics 320 x 200, 16 colours; Now draw line: CX - x, DX - y, AL - colour, BH - video page mov cx,99 mov bh,0 mov dx,150 mov ah,0CHLine: inc cx int 10H cmp cx,250 jl Line ; Repeat until cx > 250

mov ah,0 ; Wait for keypress int 16H

mov ah,0 ; Replace original video mode mov al,[savemode] int 10H

mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised datasavemode db 0

Page 84: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

84

3F03 - 80x86 assembler

; DrawLn2.asmSTART: mov ah,0FH ; Get video info int 10H mov [savemode],al ; Save the current video mode mov ah,0 mov al,0DH int 10H ; switch to graphics 320 x 200, 16 colours mov ah,0 ; Wait for keypress int 16H; Now draw line: CX - x, DX - y, AL - colour, BH - video page mov bh,0 mov ah,0CH mov dx,191Yloop: dec dx mov cx,9Line: inc cx int 10H cmp cx,310 ; Do new x until x > 310 jl Line cmp dx,10 ; Do new Y until Y < 10 jg Yloop mov ah,0 ; Wait for keypress int 16H mov ah,0 ; Replace original video mode mov al,[savemode] int 10H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised datasavemode db 0

x

y

Page 85: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

85

3F03 - 80x86 assembler; DrawLn3.asmSTART: mov ah,0FH ; Get video info int 10H mov [savemode],al ; Save the current video mode mov ah,0 mov al,0DH int 10H ; switch to graphics 320 x 200, 16 colours mov ah,0 int 16H; Now draw line: CX - x, DX - y, AL - colour, BH - video page mov bh,0 mov ah,0CH mov dx,191Yloop: dec dx mov cx,9 mov al,3Line: inc cx int 10H cmp cx,310 jl Line cmp dx,10 jg Yloop mov ah,0 int 16H mov ah,0 mov al,[savemode] int 10H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised datasavemode db 0

Pixel colour

Page 86: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

86

3F03 - 80x86 assembler

; DrawLn4.asm.. .. .. .. ; Now draw line: CX - x, DX - y, AL - colour, BH - video page mov bh,0 mov ah,0CH mov dx,191Yloop: dec dx mov cx,9 xor al,al Zero ALLine: inc al Next colour cmp al,15 jl NoReset xor al,al Zero AL if colour > 15 - starts from 0 againNoReset: inc cx int 10H cmp cx,310 jl Line cmp dx,10 jg Yloop mov ah,0 int 16H mov ah,0 mov al,[savemode] int 10H mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised datasavemode db 0

Page 87: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

87

3F03 - 80x86 assembler

; DrawLn5.asm.. .. .. .. ; Now draw line: CX - x, DX - y, AL - colour, BH - video page mov bh,0 mov ah,0CH mov dx,191Yloop: dec dx mov cx,9 xor al,al Zero ALLine: inc al Next colour inc cx int 10H cmp cx,310 jl Line

cmp dx,10 jg Yloop

mov ah,0 int 16H

mov ah,0 mov al,[savemode] int 10H

mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised datasavemode db 0

Page 88: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

88

3F03 - 80x86 assembler

• Memory-Mapped Graphics Mode 13H - 320 x 200 256 colours Two-dimensional array of bytes Each byte describes a single pixel at x,y

• The bytes for each row (y) are contiguous

The video colour palette is at port 3C8H The RGB entries are sent to port 3C9H Video buffer starts at A000H

Page 89: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

89

3F03 - 80x86 assembler

• Memory-Mapped Graphics0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0

0,1 1,1 2,1 3,1 4,1 5,1 6,1 7,1 8,1 9,1

0,2 1,2 2,2 3,2 4,2 5,2 6,2 7,2 8,2 9,2

0,3 1,3 2,3 3,3 4,3 5,3 6,3 7,3 8,3 9,3

0,4 1,4 2,4 3,4 4,4 5,4 6,4 7,4 8,4 9,4

0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 0,1 1,1

0 1 2 3 4 5 6 7 8 9 10 11

2,1 3,1 4,1 5,1 6,1 7,1 8,1 9,1 0,2 1,2 2,2 3,2

12 13 14 15 16 17 18 19 20 21 22 23

4,2 5,2 6,2 7,2 8,2 9,2 0,3 1,3 2,3 3,3 4,3 5,3

24 25 26 27 28 29 30 31 32 33 34 35

6,3 7,3 8,3 9,3 0,4 1,4 2,4 3,4 4,4 5,4 6,4 7,4

36 37 38 39 40 41 42 43 44 45 46 47

8,4 9,4

48 49

10 x 5 pixelsConvert pixel location to bytenumber in video segment.Given x,y: byte number is y*10 + x

Start of video segment

Page 90: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

90

3F03 - 80x86 assembler

; MemMap1.asm

segment code

..start:

mov ax,data Have to do it as an EXE program

mov ds,ax since we need access to memory

mov ax,stack outside the limits of a COM program

mov ss,ax

mov sp,stacktop

; store original video mode

mov ah,0Fh

int 10H

mov [savemode],al

; change video mode to 13H

mov ah,0

mov al,13H

int 10H

; wait for keypress

mov ah,0

int 16H

; set es to point to video buffer

mov ax,0A000H

mov es,ax

Page 91: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

91

3F03 - 80x86 assembler

; draw pixel at cx,bx

mov bx,191

Yloop:

dec bx

mov ax,bx

mov dx,320

mul dx ; multiply ax by 320 for each y

mov di,ax ; put start byte for each row in di

mov cx,9 ; x value

xor al,al ; colour value in current palette

add di,cx ; include x in start address

Line:

inc al ; next colour

inc cx ; next pixel

inc di ; increment address in row

mov [es:di],al ; move pixel colour into pixel byte address

cmp cx,310

jl Line

cmp bx,10

jg Yloop

Page 92: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

92

3F03 - 80x86 assembler

mov ah,0

int 16H ; wait for keypress

mov ah,0

mov al,[savemode]

int 10H ; restore original video mode

mov ax, $4C00 ; Prepare to exit

int 0x21 ; Terminate prog

segment data

savemode resb 1

segment stack stack

resb 256

stacktop:

Page 93: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

93

3F03 - 80x86 assembler

; MemMap2.asm

segment code

.. .. .. ..

; loop 5 times

xor si,si

Count:

inc si

mov bp,si

and bp,0001

; draw pixel at cx,bx

mov bx,191

Yloop:

dec bx

mov ax,bx

mov dx,320

mul dx ; multiply ax by 320 for each y

mov di,ax ; put start byte for each row in di

mov cx,9 ; x value

xor al,al ; colour value in current palette

add di,cx ; include x in start address

Page 94: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

94

3F03 - 80x86 assembler

Line:

cmp bp,1 ;

jne Zero ; don't increment colour if si is even

inc al ; next colour

Zero:

inc cx ; next pixel

inc di ; increment address in row

mov [es:di],al ; move pixel colour into pixel byte address

cmp cx,310

jl Line

cmp bx,10

jg Yloop

cmp si,5

jl Count

.. .. .. ..

segment data

savemode resb 1

segment stack stack

resb 256

stacktop:

Page 95: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

95

3F03 - 80x86 assembler

Compare MemMap2 with DrawLn6, where DrawLn6 is the same as DrawLn5 but with the same loop (5 times) as in MemMap2.

DrawLn6 is clearly slower - it flickers between iterations. MemMap2 shows no such flicker.

Page 96: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

96

3F03 - 80x86 assembler

Screen display from DrawLn

Screen display from MemMap

Page 97: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

97

3F03 - 80x86 assembler

Screen display from DrawLn

Screen display from MemMap

Single pixel “blip” caused by pressing PrtSc

Page 98: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

98

3F03 - 80x86 assembler

To change the palette we need to write to the hardware ports.

Colour 0 in the palette is always the background colour.

• To change the background colour to a mid-range blue (for instance), we set colour 0 to 35.

Write 0 to port 3C8H (colour #) Write 0 to port 3C9H (Red) Write 0 to port 3C9H (Green) Write 35 to port 3C9H (Blue)

Page 99: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

99

3F03 - 80x86 assembler

; MemMap4.asmsegment code.. .. .. ..; change video mode to 13H mov ah,0 mov al,13H int 10H; change background colour to a dark shade of blue mov dx,3c8H mov al,0 out dx,al ; colour 0 in the palette means background ; colour. Specify RGB values next. mov dx,3c9H mov al,0 ; red set to 0 out dx,al mov al,0 ; green set to 0 out dx,al mov al,35 ; set blue to 35 (max is 63) out dx,al; wait for keypress mov ah,0 int 16H; set es to point to video buffer mov ax,0A000H mov es,ax; draw pixel at cx,bx.. .. .. ..segment datasavemode resb 1

segment stack stack resb 256stacktop:

Page 100: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

100

3F03 - 80x86 assembler

We can also modify any other colours in the palette.

For example, we can modify colours so that the palette contains mainly shades of red:

• Colour Red Green Blue

i=1,63 i 0 0

63+i i 15 15

126+i i 30 30

189+i i 45 45

253,255 No Change to these

Page 101: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

101

3F03 - 80x86 assembler

; MemMap5.asmsegment code.. .. .. ..; change background colour to a dark shade of blue mov dx,3c8H mov al,0 out dx,al ; colour 0 in the palette means background ; colour. Specify RGB values next. mov dx,3c9H mov al,0 ; red set to 0 out dx,al mov al,0 ; green set to 0 out dx,al mov al,35 ; set blue to 35 (max is 63) out dx,al; modify palette to various shades of red; first 63 values: i 0 0, i=1,63; next 63 values: i 15 15; next 63 values: i 30 30; next 63 values: i 45 45; last 3 values: unchanged mov ah,0 ; BG value mov bh,15 ; BG increment mov bl,0 ; start i (less 1) mov bp,4 ; number of major loops

Page 102: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

102

3F03 - 80x86 assembler

OuterLoop: mov cx,63 ; 63 counts in inner loopInnerLoop: inc bl mov dx,3c8H mov al,bl out dx,al mov dx,3c9H mov al,bl ; red out dx,al mov al,ah ; green out dx,al out dx,al ; blue loop InnerLoop add ah,bh ; next BG value dec bp jg OuterLoop

; wait for keypress mov ah,0 int 16H.. .. .. .. mov ax, $4C00 ; Prepare to exit int 0x21 ; Terminate prog

segment datasavemode resb 1

segment stack stack resb 256stacktop:

Page 103: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

103

3F03 - 80x86 assembler

Output of original palette colours

Output of modified palette colours

Single pixel “blip” caused by pressing PrtSc

Page 104: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

104

3F03 - 80x86 assembler

• What are the advantages and dis-advantages of accessing hardware directly rather than through the BIOS or the operating system? Advantages: speed & functionality

• Going directly to hardware is always faster rather than going through layers of system software.

• If new hardware is compatible with existing hardware but adds capabilities.

Disadvantages: compatibility• BIOS / operating system software makes hardware

from various manufacturers look the same to our applications.

Page 105: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

105

3F03 - 80x86 assembler

• What is the mapping if we have a mode that uses a maximum of 16 colours rather than 256? Each byte in memory specifies the colour of

two pixels. Setting the colour of a single pixel becomes

more complicated than the 256 colour case because we have to be certain not to disturb the “companion” pixel in the byte of interest.

The mapping from x,y coordinate to byte number is also slightly more complicated.

Page 106: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

106

3F03 - 80x86 assembler

Setting the colour of a single pixel - 16 colours

01234567

pixel npixel n+1

Current bytein memory

01234567

AND withthis mask

1 1 1 1 0 0 0 0

To set the value of pixel n (x is even):

01234567

OR with thisbyte

0 0 0 0 p3 p2 p1 p0

where p3 p2 p1 p0 are the bits specifyingthe colour of pixel n

Page 107: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

107

3F03 - 80x86 assembler

Setting the colour of a single pixel - 16 colours

01234567

pixel n-1pixel n

Current bytein memory

01234567

AND withthis mask

0 0 0 0 1 1 1 1

To set the value of pixel n (x is odd):

01234567

OR with thisbyte

0 0 0 0p3 p2 p1 p0

where p3 p2 p1 p0 are the bits specifyingthe colour of pixel n

Page 108: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

108

3F03 - 80x86 assembler

Memory map - 16 colours0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0

0,1 1,1 2,1 3,1 4,1 5,1 6,1 7,1 8,1 9,1

0,2 1,2 2,2 3,2 4,2 5,2 6,2 7,2 8,2 9,2

0,3 1,3 2,3 3,3 4,3 5,3 6,3 7,3 8,3 9,3

0,4 1,4 2,4 3,4 4,4 5,4 6,4 7,4 8,4 9,4

0,0 1,0 2,0 3,0 4,0 5,0 6,0 7,0 8,0 9,0 0,1 1,1

0 1 2 3 4 5

2,1 3,1 4,1 5,1 6,1 7,1 8,1 9,1 0,2 1,2 2,2 3,2

6 7 8 9 10 11

4,2 5,2 6,2 7,2 8,2 9,2 0,3 1,3 2,3 3,3 4,3 5,3

12 13 14 15 16 17

6,3 7,3 8,3 9,3 0,4 1,4 2,4 3,4 4,4 5,4 6,4 7,4

18 19 20 21 22 23

8,4 9,4

24

10 x 5 pixelsConvert pixel location to byte numberin video segment.Given x,y: byte number is y*10/2 + x div 2

Start of video segment

Page 109: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

109

3F03 - 80x86 assembler

• Some necessary bit manipulation Shift right

• Most processors have an instruction that provides a binary shift right capability. This shifts every bit, starting with the least significant bit, a specified number of bit positions to the right. The least significant bit(s) is(are) lost. Zeros are moved into the vacated positions at the most significant bit end of the byte. Bytes/words are described by

bn ... b3 b2 b1 b0, where n is 7 (bytes) or 15 (words) etc.

SHR AX,1 is equivalent to AX divided by 2 (integer result)

SHR AX,n is equivalent to AX divided by 2n

Shift left• SHL AX,n is equivalent to multiplication by 2n

Page 110: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

110

3F03 - 80x86 assembler

• Results of 16 colour direct mapped graphics The previous scheme does not work - why

not?• Our assumption that, in this mode, each byte

holds the colour value of two adjacent pixels, was incorrect.

• This mode comes from the EGA video card (mid 1980s), and for technical reasons, the card used “bit planes”. Each of the four bit planes represents a single bit for each pixel, so the final colour of a pixel is calculated from the bit pattern associated with that pixel on each of the planes.

Page 111: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

111

3F03 - 80x86 assembler

• 16 colour bit planes

320

01

10

xy

A0000

Page 112: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

112

3F03 - 80x86 assembler

• Hardware ports A hardware port is a device that connects the

processor to external peripherals. There are 1024 ports on the 80x86, identified

by their address: 0H - 3FFH. (Third party vendors may have ports in the range 3FFH - FFFFH. The PS/2 has ports outside 3FFH.)

They are actually special memory locations, but are not part of the conventional memory at all. (They do not take up any memory locations in the standard memory map.)

Page 113: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

113

3F03 - 80x86 assembler

• Hardware ports We already saw that we can output values to

a port:MOV DX,port number

MOV AL,byte to output to port

OUT DX,AL

Similarly, we can read inputs from a port:MOV DX,port number

IN AL,DX

We can read/write bytes/words and sometimes double words from/to ports.

Can also use “immediate” port numbers.

Page 114: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

114

3F03 - Data structures in assembler

• Creating and using data structures in assembler. We will specifically consider working with

arrays/tables in assembler. Some assemblers have the equivalent of a

“struct”. Creating data structures in such cases is similar to creating them in a high-level language.

Page 115: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

115

3F03 - Data structures in assembler

• Arrays An array is a structure such that each

element in the structure is of the same type, and the elements are numbered consecutively and stored in contiguous memory.

Examples:• array1 db 20H, 30H, 40H• array2 dw 0251H, 4521H, 3F61H• array3 times 200 db 0• array4 resw 150

Page 116: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

116

3F03 - Data structures in assembler

• Arrays Size of the array

• Could manually count elements (not practical)array1 db 23H, 41H, 55H, 26H

array1_sz db 4

• Use “current location” as we did with stringsarray2 db 23H, 41H, 55H, 26H

array2_sz equ $-array2

array3 dw 1234H, 2F43H, 3AE4H, 6A7BH

array3_sz equ ($-array3)/2

• Size is an integral part of the definitionarray4_sz equ 250

array4 resb array4_sz

Page 117: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

117

3F03 - Data structures in assembler

• Arrays Accessing array elements - many ways to

access elements. One of the more common ways is to use indexed address mode.

• Set base register to address of start of array• Use index register as offset in array

(Could just use index register to point to each element, i.e. index register contains the element’s actual address.)

Page 118: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

118

3F03 - Data structures in assembler; Arrays1.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code Section

START: mov bx,IntArray ; bx points to IntArray xor si,si ; zero si used as offset in IntArray xor cx,cx ; zero cx used as index; Initialise arrayInit: mov [bx+si],cx ; mov value of cx into array inc si inc si ; next word in array inc cx ; increment index cmp cx,10 jl Init ; if index < 10 do it again; Add elements of array xor ax,ax ; zero ax to accumulate sum xor si,si ; zero si offset in array xor cx,cx ; zero indexSum: add ax,[bx+si] ; add current array element inc si inc si ; next word in array inc cx ; increment index cmp cx,10 jl Sum ; if index < 10 do it again mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing initialised dataIntArray times 80 dw 0

Page 119: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

119

3F03 - Data structures in assembler; Arrays2.asm.. .. .. .. ..; display result mov bx,ax ; copy result into bx and bx,0F000H ; mask everything except high nybble shr bx,12 ; move high nybble into low nybble mov di,result ; start of result string mov si,digits ; start of digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into first char mov bx,ax ; copy result into bx and bx,0F00H ; mask everything except low nybble in MSB shr bx,8 ; shift into low nybble inc di ; next char position in result mov si,digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into second char mov bx,ax ; copy result into bx and bx,00F0H ; mask everything except high nybble in LSB shr bx,4 ; shift into low nybble inc di ; next char position in result mov si,digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into third char mov bx,ax ; copy result into bx and bx,000FH ; mask everything except low nybble inc di ; next char position in result mov si,digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into fourth char mov dx,result mov ah,9 int 21H ; display result string on standard output.. .. .. .. ..[SECTION .data] ; Section containing initialised dataIntArray times 80 dw 0result db '0000',CR,LF,EOSdigits db '0123456789ABCDEF'

Page 120: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

120

3F03 - Data structures in assembler

Page 121: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

121

3F03 - Data structures in assembler; Arrays3.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)

[SECTION .text] ; Code SectionCR equ 0DHLF equ 0AHEOS equ '$'

START: mov cx,0 mov di,screenstr mov ah,'*'NextChar: mov [di],ah inc di inc cx cmp cx,1600 jl NextChar

mov dx,screenstr mov ah,9 int 21H ; display result string on standard output

mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing initialised datascreenstr times 1600 db '-' db CR,LF,EOS

Page 122: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

122

3F03 - Data structures in assembler

Page 123: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

123

3F03 - Data structures in assembler; Arrays4.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)

[SECTION .text] ; Code SectionCR equ 0DHLF equ 0AHEOS equ '$'

START: mov cx,1600 mov di,screenstr mov al,'*'

cld rep stosb

mov dx,screenstr mov ah,9 int 21H ; display result string on standard output

mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing initialised datascreenstr times 1600 db '-' db CR,LF,EOS

Page 124: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

124

3F03 - Data structures in assembler

Page 125: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

125

3F03 - Data structures in assembler; Arrays5.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)

[SECTION .text] ; Code Section

CR equ 0DHLF equ 0AHEOS equ '$'START: mov cx,800 mov di,screenstr mov al,'*'

cld rep stosb

mov dx,screenstr mov ah,9 int 21H ; display result string on standard output

mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing initialised datascreenstr times 1600 db '-' db CR,LF,EOS

Modified

Page 126: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

126

3F03 - Data structures in assembler

Page 127: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

127

3F03 - Data structures in assembler

• Tables - two dimensional arrays Just like the memory-mapped graphics, the

table is stored in contiguous memory cells, not in a physical two-dimensional array.

The cells in a row are contiguous. The cells in a column are separated by one

row’s storage (number of columns multiplied by the number of bytes in each element).

Accesssing elements is pretty much the same as in single dimensioned arrays.

Page 128: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

128

3F03 - Data structures in assembler; Tables1.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code Section

START:; add elements in third column (col = 2) xor ax,ax mov [sum],ax ; initialise sum xor cx,cx ; index of row mov dx,tableA ; address of tableA in dx

NextRow: push dx mov bx,dx mov ax,Ncols ; multiply Ncols by cx mul cx pop dx add bx,ax ; address of start of row cx in bx mov si,2 ; index of third column xor ax,ax ; initialise to zero mov al,[bx+si] ; byte at address Ncols*row + col add ax,[sum] ; add in previous sum mov [sum],ax ; store sum inc cx ; next row cmp cx,Nrows jl NextRow

Page 129: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

129

3F03 - Data structures in assembler; display result mov bx,[sum] ; copy result into bx and bx,0F000H ; mask everything except high nybble shr bx,12 ; move high nybble into low nybble mov di,result ; start of result string mov si,digits ; start of digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into first char .. .. .. .. mov bx,[sum] ; copy result into bx and bx,000FH ; mask everything except low nybble inc di ; next char position in result mov si,digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into fourth char

mov dx,result mov ah,9 int 21H ; display result string on standard output.. .. .. .. ..[SECTION .data] ; Section containing initialised dataCR equ 0DHLF equ 0AHEOS equ '$'tableA db 10H, 20H, 30H, 40H, 50H db 11H, 21H, 31H, 41H, 51H db 12H, 22H, 32H, 42H, 52H db 13H, 23H, 33H, 43H, 53HNrows equ 4Ncols equ 5sum dw 0result db '0000',CR,LF,EOSdigits db '0123456789ABCDEF'

Page 130: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

130

3F03 - Data structures in assembler

Page 131: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

131

3F03 - Data structures in assembler; Tables2.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionSTART:; add elements in fourth column (col = 3) xor ax,ax mov [sum],ax ; initialise sum xor cx,cx ; index of row mov dx,tableA ; address of tableA in dx

NextRow: push dx mov bx,dx mov ax,Ncols ; multiply Ncols by cx mul cx pop dx add bx,ax ; address of start of row cx in bx mov si,3 ; index of fourth column xor ax,ax ; initialise to zero mov al,[bx+si] ; byte at address Ncols*row + col add ax,[sum] ; add in previous sum mov [sum],ax ; store sum inc cx ; next row cmp cx,Nrows jl NextRow

.. .. .. .. ..

Page 132: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

132

3F03 - Data structures in assembler; Tables3.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionSTART: xor di,di ; zero col index mov bp,sum ; bp points to sum

NextCol:; add elements in column di xor ax,ax mov [bp+di],ax ; initialise sum xor cx,cx ; index of row mov dx,tableA ; address of tableA in dx

NextRow:.. .. .. .. .. inc di ; next row cmp di,Ncols jl NextCol ; if not done all cols do next col mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised dataCR equ 0DHLF equ 0AHEOS equ '$'tableA db 10H, 20H, 30H, 40H, 50H db 11H, 21H, 31H, 41H, 51H db 12H, 22H, 32H, 42H, 52H db 13H, 23H, 33H, 43H, 53HNrows equ 4Ncols equ 5sum times Ncols dw 0result db '0000',CR,LF,EOSdigits db '0123456789ABCDEF'

Page 133: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

133

3F03 - Data structures in assembler

Page 134: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

134

3F03 - Data structures in assembler; Tables4.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionSTART: xor di,di ; zero col index mov bp,sum ; bp points to sumNextCol: push bp push di; add elements in column di xor ax,ax mov [bp+di],ax ; initialise sum xor cx,cx ; index of row mov dx,tableA ; address of tableA in dxNextRow: push dx mov bx,dx mov ax,Ncols ; multiply Ncols by cx mul cx pop dx add bx,ax ; address of start of row cx in bx mov si,di ; index of column xor ax,ax ; initialise to zero mov al,[bx+si] ; byte at address Ncols*row + col add ax,[bp+si] ; add in previous sum mov [bp+si],ax ; store sum inc cx ; next row cmp cx,Nrows jl NextRow

Page 135: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

135

3F03 - Data structures in assembler; display result mov bp,[bp+di] ; store column total mov bx,bp ; copy result into bxmov bx,bp ; copy result into bx and bx,0F000H ; mask everything except high nybble shr bx,12 ; move high nybble into low nybble mov di,result ; start of result string mov si,digits ; start of digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into first char mov bx,bp ; copy result into bxmov bx,bp ; copy result into bx and bx,0F00H ; mask everything except low nybble in MSB shr bx,8 ; shift into low nybble inc di ; next char position in result mov si,digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into second char mov bx,bp ; copy result into bxmov bx,bp ; copy result into bx and bx,00F0H ; mask everything except high nybble in LSB shr bx,4 ; shift into low nybble inc di ; next char position in result mov si,digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into third char mov bx,bp ; copy result into bxmov bx,bp ; copy result into bx and bx,000FH ; mask everything except low nybble inc di ; next char position in result mov si,digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into fourth char

Page 136: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

136

3F03 - Data structures in assembler mov dx,result mov ah,9 int 21H ; display result string on standard output

pop di pop bp

inc di ; next column cmp di,Ncols jge NoMore ; jump done this way otherwise too far jmp NextCol ; if not done all cols do next col

NoMore: mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing initialised dataCR equ 0DHLF equ 0AHEOS equ '$'tableA db 10H, 20H, 30H, 40H, 50H db 11H, 21H, 31H, 41H, 51H db 12H, 22H, 32H, 42H, 52H db 13H, 23H, 33H, 43H, 53HNrows equ 4Ncols equ 5sum times Ncols dw 0result db '0000',CR,LF,EOSdigits db '0123456789ABCDEF'

Page 137: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

137

3F03 - Data structures in assembler

So, what was wrong with our program?

Page 138: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

138

3F03 - Data structures in assembler; Tables5.asm.. .. .. ..START: xor di,di ; zero col index mov bp,sum ; bp points to sum

NextCol: Multiply by 2 when dealing with words.. .. .. .. shl di,1 mov [bp+di],ax ; initialise sum shr di,1.. .. .. .. Restore di afterwardsNextRow:.. .. .. .. mov al,[bx+si] ; byte at address Ncols*row + col shl si,1 add ax,[bp+si] ; add in previous sum mov [bp+si],ax ; store sum shr si,1.. .. .. ..; display result shl di,1 mov bp,[bp+di] ; store column total shr di,1.. .. .. .. inc di ; next column cmp di,Ncols jge NoMore ; jump done this way otherwise too far jmp NextCol ; if not done all cols do next col.. .. .. ..sum times Ncols dw 0result db '0000',CR,LF,EOSdigits db '0123456789ABCDEF'

Page 139: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

139

3F03 - Data structures in assembler

Page 140: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

140

3F03 - Data structures in assembler

• Using the array name as the base address. It is quite common to use the name of the

array/table/structure as the base address. The previous example can be rewritten using

the address of sum instead of loading it into bp.

Page 141: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

141

3F03 - Data structures in assembler; Tables6.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionSTART: xor di,di ; zero col indexNextCol: push di; add elements in column di xor ax,ax shl di,1 mov [sum+di],ax ; initialise sum shr di,1 xor cx,cx ; index of row mov dx,tableA ; address of tableA in dxNextRow: push dx mov bx,dx mov ax,Ncols ; multiply Ncols by cx mul cx pop dx add bx,ax ; address of start of row cx in bx mov si,di ; index of column xor ax,ax ; initialise to zero mov al,[bx+si] ; byte at address Ncols*row + col shl si,1 add ax,[sum+si] ; add in previous sum mov [sum+si],ax ; store sum shr si,1 inc cx ; next row cmp cx,Nrows jl NextRow

Page 142: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

142

3F03 - Data structures in assembler; display result shl di,1 mov bp,[sum+di] ; store column total shr di,1 mov bx,bp ; copy result into bx and bx,0F000H ; mask everything except high nybble shr bx,12 ; move high nybble into low nybble mov di,result ; start of result string mov si,digits ; start of digits add si,bx ; pointer to representation of nybble mov dh,[si] mov [di],dh ; copy representation into first char.. .. .. .. .. mov dx,result mov ah,9 int 21H ; display result string on standard output pop di inc di ; next column cmp di,Ncols jge NoMore ; jump done this way otherwise too far jmp NextCol ; if not done all cols do next colNoMore: mov ax, 04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.[SECTION .data] ; Section containing initialised data.. .. .. .. ..tableA db 10H, 20H, 30H, 40H, 50H db 11H, 21H, 31H, 41H, 51H db 12H, 22H, 32H, 42H, 52H db 13H, 23H, 33H, 43H, 53HNrows equ 4Ncols equ 5sum times Ncols dw 0result db '0000',CR,LF,EOSdigits db '0123456789ABCDEF'

Page 143: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

143

3F03 - Data structures in assembler

• Struc Both MASM and NASM allow us to define

structures using a Struct/Struc directive/macro.

NASM does not have any intrinsic structure support - it simply provides a mechanism for defining structures that occupy contiguous memory locations.

• In MASM, a field component of a Struct is referenced by name.field, i.e. there is an underlying support for the structure.

• In NASM, we can “fool” the assembler by defining the fields as “.field_name”.

Page 144: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

144

3F03 - Data structures in assembler

• Struc example Employee info

struc employee .name: resb 32 ; string[32] .salary: resd 1 ; 32-bit integer .date: resb 3 ; yy mm dd

endstruc

Create an instance of employee byperson: istruc employee

at employee.name db ‘A.N. Other’

at employee.salary dd 75000at employee.date db 01H,

0BH, 1AHiend

Page 145: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

145

3F03 - Data structures in assembler; Struc1.asm[BITS 16] ; Set 16 bit code[ORG 0100H] ; Set code start address to 100h (COM file)[SECTION .text] ; Code SectionCR equ 0DHLF equ 0AHEOS equ '$'

START: struc employee.name: resb 32.salary: resd 1.date: resb 3 endstruc

person: istruc employee at employee.name, db 'A.N. Other',CR,LF,EOS at employee.salary, dd 75000 at employee.date, db 01H, 0BH, 1AH iend

mov dx,person+employee.name mov ah,9 int 21H ; display result string on standard output

mov ax,04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing data

Page 146: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

146

3F03 - Data structures in assembler

Page 147: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

147

3F03 - Data structures in assembler; Struc2.asm.. .. .. ..START: struc employee.name: resb 32.salary: resd 1.date: resb 3 endstrucperson1: istruc employee at employee.name, db 'A.N. Other',CR,LF,EOS at employee.salary, dd 75000 at employee.date, db 01H, 0BH, 1AH iend mov dx,person1+employee.name mov ah,9 int 21H ; display result string on standard outputperson2: istruc employee at employee.name, db ‘N.X. Twon’,CR,LF,EOS at employee.salary, dd 79000 at employee.date, db 03H, 05H, 12H iend mov dx,person2+employee.name mov ah,9 int 21H ; display result string on standard output

mov ax,04C00H ; This DOS function exits the program int 21H ; and returns control to DOS.

[SECTION .data] ; Section containing data

Page 148: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

148

3F03 - Compiler Output

• Compilers produce assembler versions of the high-level code

• Usually the compiler just produces the assembler version as object code, but we can (usually) direct the compiler to produce an assembler listing as well

• Compilers have set methods of converting high-level code into assembler It MAY be posible to hand-code the assembler

more efficiently BUT, usually a great deal of thought has gone

into the compiler algorithms - so they tend to produce very good code

Page 149: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

149

3F03 - Compiler Output/* TestSums.c */#include <stdio.h>

void sums(int a, int b, int *c);

void sums(int a, int b, int *c){ *c = a+b;}

main(){ int a,b,c; a = 5; b = 7; sums(a,b,&c); printf("sum=%d\n",c);

}

Page 150: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

150

3F03 - Compiler OutputOutput from Visual C++

PUBLIC _sums; COMDAT _sums_TEXT SEGMENT_a$ = 8_b$ = 12_c$ = 16

Page 151: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

151

3F03 - Compiler Output_sums PROC NEAR ; COMDAT; File C:\3f03_2002\Asm\TestSums.c; Line 7

push ebpmov ebp, espsub esp, 64 ; 00000040Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-64]mov ecx, 16 ; 00000010Hmov eax, -858993460 ; ccccccccHrep stosd

; Line 8mov eax, DWORD PTR _a$[ebp]add eax, DWORD PTR _b$[ebp]mov ecx, DWORD PTR _c$[ebp]mov DWORD PTR [ecx], eax

; Line 9pop edipop esipop ebxmov esp, ebppop ebpret 0

_sums ENDP_TEXT ENDS

Page 152: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

152

3F03 - Compiler OutputPUBLIC _mainPUBLIC ??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ ; `string'EXTRN _printf:NEAREXTRN __chkesp:NEAR; COMDAT ??_C@_07JNMI@sum?$DN?$CFd?6?$AA@; File C:\3f03_2002\Asm\TestSums.cCONST SEGMENT??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ DB 'sum=%d', 0aH, 00H ;

`string'CONST ENDS; COMDAT _main_TEXT SEGMENT_a$ = -4_b$ = -8_c$ = -12

Page 153: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

153

3F03 - Compiler Output_main PROC NEAR ; COMDAT; File C:\3f03_2002\Asm\TestSums.c; Line 13

push ebpmov ebp, espsub esp, 76 ; 0000004cHpush ebxpush esipush edilea edi, DWORD PTR [ebp-76]mov ecx, 19 ; 00000013Hmov eax, -858993460 ; ccccccccHrep stosd

; Line 15mov DWORD PTR _a$[ebp], 5

; Line 16mov DWORD PTR _b$[ebp], 7

; Line 17lea eax, DWORD PTR _c$[ebp]push eaxmov ecx, DWORD PTR _b$[ebp]push ecxmov edx, DWORD PTR _a$[ebp]push edxcall _sumsadd esp, 12 ; 0000000cH

Page 154: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

154

3F03 - Compiler Output; Line 18

mov eax, DWORD PTR _c$[ebp]push eaxpush OFFSET FLAT:??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ ; `string'call _printfadd esp, 8

; Line 20pop edipop esipop ebxadd esp, 76 ; 0000004cHcmp ebp, espcall __chkespmov esp, ebppop ebpret 0

_main ENDP_TEXT ENDS

Page 155: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

155

3F03 - Compiler Output

• Consider the stack as we set up the call to sums in main, and then as sums is executed we will not consider the details after sums has

completed calculating the sum and placing it in the appropriate location in the stack

Page 156: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

156

3F03 - Compiler Output

push ebp

Stack frame

ebp esp

Page 157: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

157

3F03 - Compiler Output

mov ebp,esp

Stack frame

ebpesp ebp

Page 158: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

158

3F03 - Compiler Output

sub esp,76

Stack frame

ebp ebp

esp

76 bytes

Page 159: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

159

3F03 - Compiler Output

push ebx

Stack frame

ebp ebp

esp

76 bytes

ebx

Page 160: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

160

3F03 - Compiler Output

push esi

Stack frame

ebp ebp

esp

76 bytes

ebxesi

Page 161: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

161

3F03 - Compiler Output

push edi

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

Page 162: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

162

3F03 - Compiler Output

lea edi, dword ptr [ebp-76]mov ecx,19mov eax,-858993460rep stosd

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

11001100110011001100110011001100

11001100110011001100110011001100

Page 163: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

163

3F03 - Compiler Output

mov dword ptr _a$[ebp],5

_a$ = -4 in main

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

11001100110011001100110011001100

5

Page 164: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

164

3F03 - Compiler Output

mov dword ptr _b$[ebp],7

_b$ = -8 in main

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

Page 165: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

165

3F03 - Compiler Output

lea eax,dword ptr _c$[ebp]

_c$ = -12 in main

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

eax

-12 bytes

Page 166: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

166

3F03 - Compiler Output

push eax

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptr

Page 167: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

167Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptr

ecx-8 bytes

3F03 - Compiler Output

mov ecx,dword ptr _b$[ebp]

Page 168: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

168

3F03 - Compiler Output

push ecx

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7

ecx

Page 169: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

169Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7

ecxedx

-4

3F03 - Compiler Output

mov edx,dword ptr _a$[ebp]

Page 170: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

170

3F03 - Compiler Output

push edx

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

edxecx

Page 171: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

171

3F03 - Compiler Output

push ebp

now in sums

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main)

edxecx

Page 172: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

172

3F03 - Compiler Output

mov ebp,esp

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

edxecx

Page 173: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

173

3F03 - Compiler Output

sub esp,64

Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

edxecx

Page 174: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

174

3F03 - Compiler Output

push ebx

Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebx

edxecx

Page 175: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

175

3F03 - Compiler Output

push esi

Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebxesi

edxecx

Page 176: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

176

3F03 - Compiler Output

push edi

Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebxesiedi

edxecx

Page 177: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

177

3F03 - Compiler Output

lea edi,dword ptr [ebp-64]mov ecx,16mov eax, -858993460rep stosd

Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebxesiedi

11001100110011001100110011001100

11001100110011001100110011001100

... ...11001100110011001100110011001100

... ...

edxecx

Page 178: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

178Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebxesiedi

11001100110011001100110011001100

11001100110011001100110011001100

... ...11001100110011001100110011001100

... ...

eax

edxecx

4 bytes

3F03 - Compiler Output

mov eax,dword ptr _a$[ebp]

_a$ = 4 in sums

Page 179: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

179Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebxesiedi

11001100110011001100110011001100

11001100110011001100110011001100

... ...11001100110011001100110011001100

... ...

eax+eax

edxecx

8 bytes

3F03 - Compiler Output

add eax,dword ptr _b$[ebp]

_b$ = 8 in sums

Page 180: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

180Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebxesiedi

11001100110011001100110011001100

11001100110011001100110011001100

... ...11001100110011001100110011001100

... ...

eax+eaxecx

edxecx

12 bytes

3F03 - Compiler Output

mov ecx,dword ptr _c$[ebp]

_c$ = 12 in sums

Page 181: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

181

3F03 - Compiler Output

mov dword ptr [ecx],eax

Stack frame

ebp ebp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

esp

64 bytes

ebxesiedi

11001100110011001100110011001100

11001100110011001100110011001100

... ...11001100110011001100110011001100

... ...

eax+eaxecx

12

edx

Page 182: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

182

3F03 - Compiler Output

• What can we learn from the above listings? C prefixes identifiers with _ In calling sub-programs:

• A stack frame is established in which parameters are placed on the stack in the order from right to left

• bp or ebp is used only as the base pointer - to point to sp when the call is invoked

• specific registers are protected - ebp, ebx, esi, edi

Page 183: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

183

3F03 - Compiler Output

/* TestSum2.c */#include <stdio.h>

int sums(int a, int b);

int sums(int a, int b){ return (a+b);}

main(){ int a,b; a = 5; b = 7; printf("sum=%d\n",sums(a,b));

}

Page 184: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

184

3F03 - Compiler Output_TEXT SEGMENT_a$ = 8_b$ = 12_sums PROC NEAR ; COMDAT; File C:\3F03_2002\ASM\TestSum2.c; Line 7

push ebpmov ebp, espsub esp, 64 ; 00000040Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-64]mov ecx, 16 ; 00000010Hmov eax, -858993460 ; ccccccccHrep stosd

; Line 8mov eax, DWORD PTR _a$[ebp]add eax, DWORD PTR _b$[ebp]

; Line 9pop edipop esipop ebxmov esp, ebppop ebpret 0

_sums ENDP_TEXT ENDS

Page 185: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

185

3F03 - Compiler Output; COMDAT _main_TEXT SEGMENT_a$ = -4_b$ = -8_main PROC NEAR ; COMDAT; File C:\3F03_2002\ASM\TestSum2.c; Line 13

push ebpmov ebp, espsub esp, 72 ; 00000048Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-72]mov ecx, 18 ; 00000012Hmov eax, -858993460 ; ccccccccHrep stosd

; Line 15mov DWORD PTR _a$[ebp], 5

; Line 16mov DWORD PTR _b$[ebp], 7

; Line 17mov eax, DWORD PTR _b$[ebp]push eaxmov ecx, DWORD PTR _a$[ebp]push ecxcall _sums

Page 186: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

186

3F03 - Compiler Output

add esp, 8push eaxpush OFFSET FLAT:??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ ; `string'call _printfadd esp, 8

; Line 19pop edipop esipop ebxadd esp, 72 ; 00000048Hcmp ebp, espcall __chkespmov esp, ebppop ebpret 0

_main ENDP_TEXT ENDSEND

Page 187: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

187

3F03 - Compiler Output

• What about c functions that return values? The parameters are handled as previously

noted. Scalar returned values are returned in AX or

DX:AX.

Page 188: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

188

3F03 - Compiler Output

• How about gcc? In case we get the idea that the interface

details we observe using Visual C++ are restricted to that implementation (it should not be), we can also examine output from gcc.

To this end, we can examine the assembler listing generated for TestSums.c by gcc.

The listing is shown next.

Page 189: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

189

3F03 - Compiler Output

.file "TestSums.c"gcc2_compiled.:___gnu_compiled_c:.text

.p2align 2.globl _sums_sums:

pushl %ebpmovl %esp,%ebppushl %ebxmovl 16(%ebp),%eaxmovl 8(%ebp),%edxmovl 12(%ebp),%ecxleal (%ecx,%edx),%ebxmovl %ebx,(%eax)

L1:movl -4(%ebp),%ebxleaveret

LC0:.ascii "sum=%d\12\0".p2align 2

gcc uses AT&T assembler formatfor the generated assembler.

ins source,destination

ins will have b, w, or l as a suffix:b - byte 8 bitsw - word 16 bitsl - long 32 bits

% precedes registersn(%eax) means %eax+n

Page 190: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

190

3F03 - Compiler Output.globl _main_main:

pushl %ebpmovl %esp,%ebpsubl $12,%espmovl $5,-4(%ebp)movl $7,-8(%ebp)leal -12(%ebp),%eaxpushl %eaxmovl -8(%ebp),%eaxpushl %eaxmovl -4(%ebp),%eaxpushl %eaxcall _sumsaddl $12,%espmovl -12(%ebp),%eaxpushl %eaxpushl $LC0call _printfaddl $8,%esp

L2:leaveret

clean up stack etc

Page 191: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

191

3F03 - Compiler Output

• So, gcc interfaces work in exactly the same way as do the interfaces in Visual C++ The gcc compiler does not fill the stack frame

between ebp and esp with a known value as does Visual C++

Page 192: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

192

3F03 - Inline Assembler

• Inline assembler Many high-level language compilers allow us to

write blocks of assembler code within a sub-program

The advantage of inline assembler is that the interfacing between programs is handled by the high-level language

The assembler instructions available to be used inline usually forms a subset of the full set of instructions available to be used if we are writing the entire sub-program in assembler

Examples in Visual C++ and gcc follow -

Page 193: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

193

3F03 - Inline Assembler/* TestSum3.c - Visual C++ */#include <stdio.h>

int sums(int a, int b);

int sums(int a, int b){ int r; _asm {

mov eax, DWORD PTR 8[ebp] add eax, DWORD PTR 12[ebp] mov r, eax

} return (r);}

main(){ int a,b; a = 5; b = 7; printf("sum=%d\n",sums(a,b));

}

identifier signals an inlineassembler block enclosedwithin { }

Page 194: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

194

3F03 - Inline Assembler

• The previous example does not show why inline assembler is useful, since we used our knowledge of how the interface works to access the parameters.

• This was done to demonstrate the actual inline process.

• The good news is that we could re-write the program as follows:

Page 195: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

195

3F03 - Inline Assembler/* TestSum5.c - Visual C++ */#include <stdio.h>

int sums(int a, int b);

int sums(int a, int b){ int r; _asm {

mov eax, aadd eax, bmov r, eax

} return (r);}

main(){ int a,b; a = 5; b = 7; printf("sum=%d\n",sums(a,b));

}

In an inline assembler blockwe can directly referenceexisting identifiers!

Page 196: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

196

3F03 - Inline Assembler

• gcc also allows inline assembler as shown on the next slide.

Page 197: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

197

3F03 - Inline Assembler/* TstSum4.c - gcc */#include <stdio.h>

int sums(int a, int b);

int sums(int a, int b){ asm volatile ( "movl %0, %%eax" "\n\t" "addl %1, %%eax" "\n\t" : : "r" (a), "r" (b) );}

main(){ int a,b; a = 5; b = 7; printf("sum=%d\n",sums(a,b));

}

gcc inline is clumsy - asm (inst :outputs :inputs :clobbered)

optional

optional formatting codes

I/O: %0 is first one, etc

registers need %% prefix

first input is a register

Page 198: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

198

3F03 - Inline Assembler

• What about other languages?• For example, Delphi is an integrated

environment that implements an object Pascal compiler and supports inline assembler.

Page 199: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

199

3F03 - Inline Assembler

Page 200: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

200

3F03 - Inline Assembler

Page 201: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

201

3F03 - Inline Assembler

Page 202: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

202

3F03 - Inline Assembler

Page 203: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

203

3F03 - Interfacing Assembler

• Don’t assume that all languages have the same rules for setting up the interface between procedures.

• In fact, Pascal and C use a different order for placing parameters on the stack.

• Pascal does not prefix identifiers with _.• Also, the different languages (and

sometimes different implementations of the same language) have different rules for which registers must be protected.

Page 204: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

204

3F03 - Interfacing Assembler

• Calling assembler sub-programs from a high-level language: Principles for C:

• Know which memory model is being used• Protect base pointer• Copy stack pointer into base pointer• Set up local stack allowing for all local variables• C identifiers must be prefixed by _• Load parameters onto stack, right to left• Return values in AX• Protect ebx, esi, edi• Before returning from call, restore ebx, esi, edi• Restore stack pointer• Restore base pointer• Link assembled code (.obj) with rest of object code

Page 205: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

205

3F03 - 80x86 Conclusions

• Final comments Note that a major difference between many

high-level languages and assembler is that the assembler does not support type checking.

We have not considered floating-point operations.

We have not discussed macros which are a major contribution to productivity in assembler programming.

We have concentrated on principles and concepts.

Page 206: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

206

3F03 - Introduction to MIPS

• MIPS 32 registers - prefixed by $ “simple” instructions many instructions use 3 operands

• add $t0, $s1, $s2$t0 = $s1 + $s2

special registers• $a0-$a3: registers for passing parameters• $v0-$v1: registers for returning results• $ra: register that holds the return

address protected registers

• $s0-$s7: must be protected by callee unprotected registers

• $t0-$t9: need not be protected by callee

Page 207: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

207

3F03 - Introduction to MIPS

• MIPS Register type addressing mode

• all operands are registers Immediate type addressing mode

• one of the operands is a constant Jump type addressing mode

• pseudoindirect - 26 bit address field Base or displacement addressing mode

• operand is a memory address, sum of register and a constant

PC-relative addressing mode• address is an offset to the PC address

Page 208: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

208

3F03 - Introduction to MIPS

• MIPS Instruction categories

• Arithmetic add (add), subtract(sub), add immediate (addi)

• Data transfer load word (lw), store word (sw), store byte (sb), load upper

immediate (lui), copy register to register (move)• Conditional branch

branch on equal (beq), branch on not equal (bne), set on less than (slt), set less than immediate (slti)

• Unconditional jump jump (j), jump register (jr), jump and link (jal)

Page 209: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

209

3F03 - Introduction to MIPS

• Simple example

.textmain:

li $s0, 62 # fli $s1, 55 # gli $s2, 7 # hli $s3, 96 # kadd $t0, $s0, $s1 # f + gadd $t1, $s2, $s3 # h + ksub $s0, $t0, $t1 # (f + g) - (h +

k)jr $ra # unconditional

jump# to return

address

Page 210: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

210

3F03 - Introduction to MIPS

• We can use SPIM to simulate the MIPS hardware. SPIM is available for the major operating systems. We will use the PC version.

Page 211: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

211

3F03 - Introduction to MIPS

Page 212: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

212

3F03 - Introduction to MIPS

• System Calls load system call code into $v0 load arguments into $a0-$a3 values returned in $v0 (if necessary)

see Patterson & Hennessy, pages A-48/49

Page 213: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

213

3F03 - Introduction to MIPS

• Back to the example

.datastr:

.asciiz “Result is: “

.textmain:

li $s0, 62 # fli $s1, 55 # gli $s2, 7 # hli $s3, 96 # kadd $t0, $s0, $s1 # f + gadd $t1, $s2, $s3 # h + ksub $s0, $t0, $t1 # (f + g) - (h + k)li $v0, 4 # for print_strla $a0, str # address of strsyscall # print the stringli $v0, 1 # for print_intmove $a0, $s0 # put result in $a0syscall # print resultjr $ra # unconditional jump

# to return address

Page 214: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

214

3F03 - Introduction to MIPS

• Example.data # data section

theArray: # theArray is an address in memory and.space 160 # has 160 bytes available for use.text # code section

main: # main programli $t6, 1 # load imediate value 1 into register t6li $t7, 4 # load immediate value 4 into register t7sw $t6, theArray($0) # store word from register t6 in theArray[0]sw $t6, theArray($t7) # store word from register t6 in theArray[0+t7]li $t0, 8 # load immediate value 8 into register t0

loop: # labeladdi $t3, $t0, -8 # add immediate value -8 to value in register t0

# and store in register t3addi $t4, $t0, -4 # add immediate value -4 to value in register t0

# and store in register t4lw $t1, theArray($t3) # load word from theArray[0+t3] in register t1lw $t2, theArray($t4) # load word from theArray[0+t4] in register t2add $t5, $t1, $t2 # add values in registers t1 and t2 in register t5sw $t5, theArray($t0) # store word from register t5 in theArray[0+t0]addi $t0, $t0, 4 # add immediate value 4 to value in register t0

# and store in register t0blt $t0, 160, loop # branch to loop if value in register t0 < 160jr $ra # unconditionally jump to address in register ra

Page 215: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

215

3F03 - Introduction to MIPS

• So what does the previous example do? We can re-comment the example, being more

specific about the effect of each statement

Page 216: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

216

3F03 - Introduction to MIPS• Specific annotation for example

.data # data sectiontheArray: # theArray is an address in memory and

.space 160 # has 160 bytes available for use

.text # code sectionmain: # main program

li $t6, 1 # load imediate value 1 into register t6# initial value = 1

li $t7, 4 # load immediate value 4 into register t7sw $t6, theArray($0) # store word from register t6 in theArray[0]

# f(0) = 1sw $t6, theArray($t7) # store word from register t6 in theArray[0+t7]

# f(1) = 1li $t0, 8 # load immediate value 8 into register t0

# address of f(n)loop: # label

addi $t3, $t0, -8 # add immediate value -8 to value in register t0# and store in register t3# address of f(n-2) in t3

addi $t4, $t0, -4 # add immediate value -4 to value in register t0# and store in register t4# address of f(n-1) in t4

lw $t1, theArray($t3) # load word from theArray[0+t3] in register t1# load f(n-2) into t1

lw $t2, theArray($t4) # load word from theArray[0+t4] in register t2# load f(n-1) into t2

add $t5, $t1, $t2 # add values in registers t1 and t2 in register t5# f(n-2) + f(n-1) into t5

sw $t5, theArray($t0) # store word from register t5 in theArray[0+t0]# f(n) = f(n-2) + f(n-1) stored in t5

addi $t0, $t0, 4 # add immediate value 4 to value in register t0# and store in register t0# address of f(n+1)

blt $t0, 160, loop # branch to loop if value in register t0 < 160# 160 bytes is 40 elements

jr $ra # unconditionally jump to address in register ra

Page 217: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

217

3F03 - Introduction to MIPS

• So what does it do?

It computes and stores the Fibonacci numbers:

fib(0) = 1fib(1) = 1fib(n) = fib(n-1) + fib(n-2), n=2,3,4,…,39

Page 218: Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006

218

3F03 - Introduction to MIPS

• MIPS MIPS is a load-store architecture

• Only load and store instructions access memory RISC - Reduced Instruction Set Computer

• fixed instruction length• load-store architecture• limited addressing modes• limited operations

Well-suited for use by compilers rather than by human coders