machine language - 3f03 - mcmaster university

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

Upload: others

Post on 18-Dec-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Machine Language - 3F03

Alan WassyngITB 166

[email protected] - April 2006

1

3F03 - Overview

• Before we start, let’s refresh ourknowledge of C. Consider how wecould solve the following problem in C:

We have an analog I/O hardware modulethat provides 12-bit analog input andoutput capability for 8 inputs and 8 outputson a PC.Construct a C routine to read any one ofthe analog inputs - 0..7

int GetAI(int IOpoint);

2

3F03 - Overview

• How the analog board worksSet the IO channel using the IOpointSet the control register according to the IOchannel LSBRead the value at the data register - thatvalue is the LSB of the analog inputSet the control register according to the IOchannel MSBRead the value at the data register - thatvalue is the MSB of the analog input

3

3F03 - Overview

• DefinitionsBase address = 0100hControl register address = Base address+1Data register address = Base addressLSB of IO channel = IOpoint*2MSB of IO channel = IOpoint*2+1

4

3F03 - Overview

• Required StepsSo, create OutPort(p, val) that sets the value ofhardware port p, to val, and create InPort(p) thatreads the value of port p. Then the followingsequence 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

5

3F03 - Overview

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

6

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

7

3F03 - Overview

• So, what did you find?It is not obvious as to how to access thehardware from within a standard Cprogram.

• High-level languages were developed toenable programmers to work at a higherlevel of abstraction without concerningthemselves too much about thehardware - but it is sometimesnecessary to “sink” to the hardwarelevel.

8

3F03 - Overview

• Course RulesTAs

• 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 gradeis < 50% it will be used as the total grade for the course.

9

3F03 - Overview

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

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

10

3F03 - Overview

• Course RulesAssignments & Exams

• Do - explain what you have done and what youhave 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

11

3F03 - Overview

• Course ObjectivesMachine level programming concepts.The role of assembler programming in thecurrent software industry.How the concepts of assemblerprogramming can help you even thoughyou, personally, program all the time in ahigh-level language.

12

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

13

3F03 - Assembler Basics

• Machine LanguageA typical processor

RegistersArithmetic & Logic

Control

Data Path

Registers aresimply fastmemorylocations.Some may havespecific roles toplay, whileothers may bemore general.

14

3F03 - Assembler Basics

• Word sizeThe word size of a processor is the largestnumber of bits it works with as a unit.The original Intel 8088/8086 has a 16-bitword size. The registers are 16 bits andthe 8086 works with 16 bit words inmemory. The 8088 has an 8-bit data bus.The Intel Pentium chips have a 32-bit wordsize.Later we’ll see how Intel retains backwardcompatibility in its processors.

15

3F03 - Assembler Basics

• Machine InstructionsEach processor has its own unique set ofregisters and instructions.Examples:10110100 00001001 moves 09 into high

byte of reg AX01000011 increment reg BX

by 1Note the difference in these instructions.The first one required 2 bytes, the secondonly 1 byte. (They also require a differentnumber of clock cycles.)

16

3F03 - Assembler Basics

• AssemblerConverts easier to read/create mnemonicsand operands to machine language.One-to-one correspondence betweenassembler statements and machineinstructions - usually, since sometimes anassembler statement is an instruction tothe assembler program itself.Assemblers are often augmented by macroextensions.

17

3F03 - Assembler Basics

• 80x86 assembler (Intel notation)General structure

label mnemonic operand(s) commentSET: MOV AX,BX ;move BX into AX

dest,source

ADD AX,BX ;add BX to AX and;store in AX

18

3F03 - Assembler Basics

• 80x86 Layout

8 bits16 bits32 bits

EAX

EBX

ECX

EDXESIEDIESPEBP

AH AL

BH BL

CH

DH DL

CL

CSDSSSESFSGS

19

3F03 - Assembler Basics

• 80x86 “Special” RegistersIP - The instruction pointer

• points to next instruction to be exceutedFLAGS (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

20

3F03 - Computer arithmetic, binary, octal, hex

• IntegersBe specific about size (number of bits) andsignage (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

21

3F03 - Computer arithmetic, binary, octal, hex

• OctalDEC used octal for its very successful mini-computers - PDP and VAX. Not usedmuch in current architectures.

• HexAlmost everything we do in the assemblerwill be in Hex. Get familiar with it!Each hex digit represents a nybble

0 - F represents 0000 - 1111

22

3F03 - 80x86 assembler

• AddressesAddresses in the 8088, 8086, 80286 worldare written in the form

• segment:offset• segments start on 16 byte boundaries - the first

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

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

Addresses in the 80386 (and later) world canmimic the earlier, restricted addresses, or can berepresented by a “flat” model so that segments arenot necessary.

23

3F03 - 80x86 assembler

• AddressesConsider the address 0001:0032

0000

0001

0002

Segment 0

Segment 1

Segment 2

0001:0032 = 0000:0042 = 0002:0022

24

3F03 - 80x86 assembler

• Open a DOS window and run Debug.

25

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

26

3F03 - 80x86 assembler

• Debug examples

27

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

28

3F03 - 80x86 assembler

INC - Increment

Usage: INC dest Modifies flags: AF OF PF SF ZF

Adds one to destination unsigned binary operand.

Clocks SizeOperands 808x 286 386 486 Bytes

reg8 3 2 2 1 2reg16 3 2 2 1 1reg32 3 2 2 1 1mem 15+EA 7 6 3 2-4 (W88=23+EA)

29

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

30

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 thedestination. If CF is set, a 1 is added to thedestination.

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

31

3F03 - 80x86 assembler

If we use ADC rather than ADD everythingworks just fine.

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

32

3F03 - 80x86 assembler

• URL for Intel 80x86 Instructions

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

33

3F03 - 80x86 assembler

• Some registers have special rolesThe names of the general registers werechosen to reflect the specific roles they canplay:

• AX - accumulator (brief history)• BX - base register• CX - counter register• DX - data register

34

3F03 - 80x86 assembler

• Loop example - in Debug1478:0100 MOV AX,01478:0103 MOV CX,51478:0106 INC AX1478:0107 LOOP 0106

35

3F03 - 80x86 assembler

• Loop example - in Debug1478:0100 MOV AX,01478:0103 MOV CX,51478:0106 INC AX1478:0107 LOOP 0106

counter

36

3F03 - 80x86 assembler

• LOOP - Decrement CX and Loop if CXNot Zero

Usage: LOOP labelModifies Flags: NoneDecrements CX by 1 and transfers control to"label" if CX is not Zero. The "label" operandmust be within -128 or 127 bytes of theinstruction following the loop instruction

37

3F03 - 80x86 assembler

• Loop example - in AssemblerMOV AX,0MOV CX,5

LBL: INC AXLOOP LBL

38

3F03 - 80x86 assembler

• Loop example - in AssemblerMOV AX,0MOV CX,5

LBL: INC AXLOOP LBL

39

3F03 - 80x86 assembler

• Loop example - source fileCreate 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

40

3F03 - 80x86 assembler

• Loop example - assemble sourceAssemble and link file using NASMNASM TestLoop.asm -o TestLoop.com

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

41

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

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

How is this numbercalculated?

43

3F03 - 80x86 assembler

• Loop, with CMP and Jxx - in Debug1478:0100 MOV AX,01478:0103 MOV CX,51478:0106 INC AX

1478:0107 DEC CX1478:0108 CMP CX,01478:010B JNZ 0106

44

3F03 - 80x86 assembler

• CMP - CompareUsage: CMP dest,srcModifies Flags: AF CF OF PF SF ZFSubtracts source from destination andupdates the flags but does not save result.Flags can subsequently be checked forconditions.

45

3F03 - 80x86 assembler

• JXX - Jump Instructions TableMnm Meaning Jump ConditionJA Jump if Above CF=0 & ZF=0JAE Jump if Above or Equal CF=0 JB Jump if Below CF=1 … … ...

46

3F03 - 80x86 assembler

• Loop, with CMP and Jxx - in Debug1478:0100 MOV AX,01478:0103 MOV CX,51478:0106 INC AX

1478:0107 DEC CX1478:0108 JNZ 0106

47

3F03 - 80x86 assembler

• Typical addressing modes label opcode 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

48

3F03 - 80x86 assembler

• Typical addressing modesRegister and immediate modes we have alreadyseen

MOV AX,1MOV BX,AX

register immediate

49

3F03 - 80x86 assembler

• Typical addressing modesAbsolute address mode

MOV AX,[0200]

value stored in memory location DS:0200

50

3F03 - 80x86 assembler

• Typical addressing modesRegister indirect

MOV AX,[BX]

value stored at address contained in DS:BX

51

3F03 - 80x86 assembler

• Typical addressing modesDisplacement

MOV DI,4MOV AX,[0200+DI]

value stored at DS:0204

52

3F03 - 80x86 assembler

• Typical addressing modesIndexed

MOV BX,0200MOV DI,4MOV AX,[BX+DI]

value stored at DS:0204

53

3F03 - 80x86 assembler

• Typical addressing modesMemory indirect

MOV DI,0204MOV BX,[DI]MOV AX,[BX]

If DS:0204 contains 0256,then AX will containwhatever is stored atDS:0256

54

3F03 - 80x86 assembler

• Typical addressing modesMemory indirect

MOV DI,0204MOV BX,[DI]MOV AX,[BX]

If DS:0204 contains 0256,then AX will containwhatever is stored atDS:0256

Byte addresses in memory

0200 0204

0250 0256

0256

1234

DI

BX

AX = 1234

55

3F03 - 80x86 assembler

• 80x86 RestrictionsMOV cannot move memory to memory

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

MOV cannot move a segment register intoanother segment register.MOV cannot move immediate data into asegment register.Mov cannot move an 8-bit register half into a16-bit register.In real mode, only BP, BX, SI and DI canhold an offset for memory data.

56

3F03 - 80x86 assembler

• Complications arising from memoryaccess 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. InNASM we simply use BYTE, WORD, etc.e.g. NEG BYTE [BX]

57

3F03 - 80x86 assembler

• Text strings in assemblerDefine the string (or a place holder) in thedata segment - actually we define theaddress at which the string starts as wellas the characters in the string.Move the starting address of the string intoa register, and go from there ….

58

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, "$"

59

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, "$"

60

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 "$"

61

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 "$"

62

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

63

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 $-msgmsg2 times 80 db EOS

64

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 $-msgmsg2 times 80 db EOS

65

3F03 - 80x86 assembler

• RecapThe processor understands binaryinstructions (32-bit, 16-bit etc) and workswith binary data.The basic building blocks are registerswhich are just special memory locationswithin the processor.We write programs in Assembler, which isa notation that gets translated (bysoftware) into machine language.

66

3F03 - 80x86 assembler

• AssemblerThe principal tasks performed by anassembler:

• Replace opcodes and operands by theirmachine level equivalents.

• Replace symbolic names by relocatableaddresses.

67

3F03 - 80x86 assembler

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

• In general, the assembler produces a “relocatable”object file - a file containing the machineinstructions translated from the assembler sourcecode, including “unresolved external symbols”.

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

• The loader has the task of installing theexecutable file at the correct starting locationimmediately prior to being invoked.

68

3F03 - 80x86 assembler

• Relocatable object file - opcodes plus:Import table

• Named locations that are unknown butassumed to be defined in files that will be linkedwith this one.

Relocation table• Locations within the current file that must be

modified at link time to take into account theoffset of the current file within the finalexectable program.

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

referred to in other relocatable object files.

69

3F03 - 80x86 assembler

• Complications in linkingRelocation typically involves not onlymodifying addresses simply by taking intoaccount the offset of the particular file, butmay also combine code segments into asingle code segment, and data segmentsinto a single data segment, etc.

70

3F03 - 80x86 assembler

• NASMThe Net-wide Assembler is an open sourceinitiative and is available (free) for a varietyof operating systems - DOS, Windows &Linux.The author(s) tried to make it more explicitand consistent with respect to the way inwhich it treats memory references than isMASM.It is capable of producing .COM .OBJ and.EXE files.

71

3F03 - 80x86 assembler

• NASMNASM is therefore both an assembler anda linker.In general, we usually use two separateprograms to perform these functions. Wecan do that now as well. NASM will beused as an assembler, and unless thelinking is trivial (a .COM file, or a singlesource file), we would use a linker such asALINK to link the required .OBJ files.

72

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:

73

3F03 - 80x86 assembler

NASM TestExe.asm -fobj -o TestExe.obj

Alink TestExe.obj -oEXE -o TestExe.exe

Execute: TestExe↵

Examine TestExe.exe using Debug

74

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:

75

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:

76

3F03 - 80x86 assembler

• BIOS, OS and Device DriversBasic Input-Output System provides lowlevel routines that interface to the hardwarethrough software interrupts.Typically, the OS includes routines thatbuild on the BIOS level interface to provideusers with even more capability than doesthe BIOS.Devices that were not included in the BIOShave to be serviced by Device Drivers.These provide input-output services for theparticular device.

77

3F03 - 80x86 assembler

DeviceDrivers

Hardware

BIOSDD1

DD2

App 1 App 2 App 3

Operating System

VideoKeyboardOther Hardware

78

3F03 - 80x86 assembler

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

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

2 80 x 25 2 colours text3 80 x 25 16 colours text0Dh 320 x 200 16 colours graphics12h 640 x 480 16 colours graphics13h 320 x 200 256 colours graphics6Ah 800 x 600 16 colours graphics

• Coordinates0,0 top left Xmax,Ymax bottom right

79

3F03 - 80x86 assembler

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

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

80

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 beXORed with the current contents of the pixel.This allows us to erase pixels.

81

3F03 - 80x86 assembler

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

Set graphics modeFor x = 100 to 250

• Set pixel colour• Set pixel

Wait for a key pressExit program

82

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

83

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

xy

84

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

85

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

86

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

87

3F03 - 80x86 assembler

• Memory-Mapped GraphicsMode 13H - 320 x 200 256 coloursTwo-dimensional array of bytesEach byte describes a single pixel at x,y

• The bytes for each row (y) are contiguousThe video colour palette is at port 3C8HThe RGB entries are sent to port 3C9HVideo buffer starts at A000H

88

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

89

3F03 - 80x86 assembler

; MemMap1.asmsegment 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

90

3F03 - 80x86 assembler

; draw pixel at cx,bx mov bx,191Yloop: 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 addressLine: 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

91

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 datasavemode resb 1

segment stack stack resb 256stacktop:

92

3F03 - 80x86 assembler

; MemMap2.asmsegment code.. .. .. ..; loop 5 times xor si,siCount: inc si mov bp,si and bp,0001; draw pixel at cx,bx mov bx,191Yloop: 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

93

3F03 - 80x86 assembler

Line: cmp bp,1 ; jne Zero ; don't increment colour if si is even inc al ; next colourZero: 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 datasavemode resb 1segment stack stack resb 256stacktop:

94

3F03 - 80x86 assembler

Compare MemMap2 with DrawLn6, whereDrawLn6 is the same as DrawLn5 but withthe same loop (5 times) as in MemMap2.DrawLn6 is clearly slower - it flickersbetween iterations. MemMap2 shows nosuch flicker.

95

3F03 - 80x86 assembler

Screendisplay fromDrawLn

Screendisplay fromMemMap

96

3F03 - 80x86 assembler

Screendisplay fromDrawLn

Screendisplay fromMemMap

Single pixel“blip” causedby pressingPrtSc

97

3F03 - 80x86 assembler

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

Colour 0 in the palette is always thebackground 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)

98

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:

99

3F03 - 80x86 assembler

We can also modify any other colours inthe palette.For example, we can modify colours sothat the palette contains mainly shades ofred:

• Colour Red Green Bluei=1,63 i 0 063+i i 15 15126+i i 30 30189+i i 45 45253,255 No Change to these

100

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

101

3F03 - 80x86 assemblerOuterLoop: 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:

102

3F03 - 80x86 assembler

Output oforiginal palettecolours

Output ofmodified palettecolours

Single pixel“blip” causedby pressingPrtSc

103

3F03 - 80x86 assembler

• What are the advantages and dis-advantages of accessing hardwaredirectly rather than through the BIOS orthe 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 makeshardware from various manufacturers look thesame to our applications.

104

3F03 - 80x86 assembler

• What is the mapping if we have a modethat uses a maximum of 16 colours ratherthan 256?

Each byte in memory specifies the colour oftwo pixels.Setting the colour of a single pixel becomesmore complicated than the 256 colour casebecause we have to be certain not to disturbthe “companion” pixel in the byte of interest.The mapping from x,y coordinate to bytenumber is also slightly more complicated.

105

3F03 - 80x86 assembler

Setting the colour of a single pixel - 16 colours

01234567

pixel npixel n+1

Current bytein memory

01234567AND withthis mask 1 1 1 1 0 0 0 0

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

01234567OR with this

byte 0 0 0 0 p3 p2 p1 p0

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

106

3F03 - 80x86 assembler

Setting the colour of a single pixel - 16 colours

01234567

pixel n-1pixel n

Current bytein memory

01234567AND withthis mask 0 0 0 0 1 1 1 1

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

01234567OR with this

byte 0 0 0 0p3 p2 p1 p0

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

107

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

108

3F03 - 80x86 assembler

• Some necessary bit manipulationShift right

• Most processors have an instruction that providesa binary shift right capability. This shifts every bit,starting with the least significant bit, a specifiednumber of bit positions to the right. The leastsignificant bit(s) is(are) lost. Zeros are movedinto the vacated positions at the most significantbit 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

109

3F03 - 80x86 assembler

• Results of 16 colour direct mappedgraphics

The previous scheme does not work - whynot?

• Our assumption that, in this mode, each byteholds the colour value of two adjacent pixels, wasincorrect.

• This mode comes from the EGA video card (mid1980s), and for technical reasons, the card used“bit planes”. Each of the four bit planesrepresents a single bit for each pixel, so the finalcolour of a pixel is calculated from the bit patternassociated with that pixel on each of the planes.

110

3F03 - 80x86 assembler

• 16 colour bit planes

320

200

01

10

xy

A0000

111

3F03 - 80x86 assembler

• Hardware portsA hardware port is a device that connects theprocessor to external peripherals.There are 1024 ports on the 80x86, identifiedby their address: 0H - 3FFH. (Third partyvendors 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 memoryat all. (They do not take up any memorylocations in the standard memory map.)

112

3F03 - 80x86 assembler

• Hardware portsWe already saw that we can output values toa port:

MOV DX,port numberMOV AL,byte to output to portOUT DX,AL

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

We can read/write bytes/words andsometimes double words from/to ports.Can also use “immediate” port numbers.

113

3F03 - Data structures in assembler

• Creating and using data structures inassembler.

We will specifically consider working witharrays/tables in assembler.Some assemblers have the equivalent of a“struct”. Creating data structures in suchcases is similar to creating them in a high-level language.

114

3F03 - Data structures in assembler

• ArraysAn array is a structure such that eachelement in the structure is of the same type,and the elements are numberedconsecutively and stored in contiguousmemory.Examples:

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

115

3F03 - Data structures in assembler

• ArraysSize of the array

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

• Use “current location” as we did with stringsarray2 db 23H, 41H, 55H, 26Harray2_sz equ $-array2array3 dw 1234H, 2F43H, 3AE4H, 6A7BHarray3_sz equ ($-array3)/2

• Size is an integral part of the definitionarray4_sz equ 250array4 resb array4_sz

116

3F03 - Data structures in assembler

• ArraysAccessing array elements - many ways toaccess elements. One of the more commonways 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 eachelement, i.e. index register contains theelement’s actual address.)

117

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

118

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'

119

3F03 - Data structures in assembler

120

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

121

3F03 - Data structures in assembler

122

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

123

3F03 - Data structures in assembler

124

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

125

3F03 - Data structures in assembler

126

3F03 - Data structures in assembler

• Tables - two dimensional arraysJust like the memory-mapped graphics, thetable 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 onerow’s storage (number of columns multipliedby the number of bytes in each element).Accesssing elements is pretty much thesame as in single dimensioned arrays.

127

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

128

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'

129

3F03 - Data structures in assembler

130

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

.. .. .. .. ..

131

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'

132

3F03 - Data structures in assembler

133

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

134

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

135

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'

136

3F03 - Data structures in assembler

So, what was wrong with our program?

137

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'

138

3F03 - Data structures in assembler

139

3F03 - Data structures in assembler

• Using the array name as the baseaddress.

It is quite common to use the name of thearray/table/structure as the base address.The previous example can be rewritten usingthe address of sum instead of loading it intobp.

140

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

141

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'

142

3F03 - Data structures in assembler

• StrucBoth MASM and NASM allow us to definestructures using a Struct/Strucdirective/macro.NASM does not have any intrinsic structuresupport - it simply provides a mechanism fordefining structures that occupy contiguousmemory locations.

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

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

143

3F03 - Data structures in assembler

• Struc exampleEmployee info

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

endstrucCreate an instance of employee by

person: istruc employeeat employee.name db ‘A.N. Other’at employee.salary dd 75000at employee.date db 01H, 0BH, 1AHiend

144

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

145

3F03 - Data structures in assembler

146

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

147

3F03 - Compiler Output

• Compilers produce assembler versions ofthe high-level code

• Usually the compiler just produces theassembler version as object code, but wecan (usually) direct the compiler to producean assembler listing as well

• Compilers have set methods of convertinghigh-level code into assembler

It MAY be posible to hand-code the assemblermore efficientlyBUT, usually a great deal of thought has goneinto the compiler algorithms - so they tend toproduce very good code

148

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);

}

149

3F03 - Compiler OutputOutput from Visual C++PUBLIC _sums; COMDAT _sums_TEXT SEGMENT_a$ = 8_b$ = 12_c$ = 16

150

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

pushebpmov ebp, espsub esp, 64 ; 00000040Hpushebxpushesipushedilea 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

151

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

152

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

pushebpmov ebp, espsub esp, 76 ; 0000004cHpushebxpushesipushedilea 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]pusheaxmov ecx, DWORD PTR _b$[ebp]pushecxmov edx, DWORD PTR _a$[ebp]pushedxcall_sumsadd esp, 12 ; 0000000cH

153

3F03 - Compiler Output; Line 18

mov eax, DWORD PTR _c$[ebp]pusheaxpushOFFSET 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

154

3F03 - Compiler Output

• Consider the stack as we set up the call tosums in main, and then as sums isexecuted

we will not consider the details after sums hascompleted calculating the sum and placing it inthe appropriate location in the stack

155

3F03 - Compiler Outputpush ebp

Stack frame

ebp esp

156

3F03 - Compiler Outputmov ebp,esp

Stack frame

ebpesp ebp

157

3F03 - Compiler Outputsub esp,76

Stack frame

ebp ebp

esp

76 bytes

158

3F03 - Compiler Outputpush ebx

Stack frame

ebp ebp

esp

76 bytes

ebx

159

3F03 - Compiler Outputpush esi

Stack frame

ebp ebp

esp

76 bytes

ebxesi

160

3F03 - Compiler Outputpush edi

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

161

3F03 - Compiler Outputlea edi, dword ptr [ebp-76]mov ecx,19mov eax,-858993460rep stosd

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100

11001100110011001100110011001100

162

3F03 - Compiler Outputmov dword ptr _a$[ebp],5

_a$ = -4 in main

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

1100110011001100110011001100110011001100110011001100110011001100

11001100110011001100110011001100

5

163

3F03 - Compiler Outputmov dword ptr _b$[ebp],7

_b$ = -8 in main

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

164

3F03 - Compiler Outputlea eax,dword ptr _c$[ebp]

_c$ = -12 in main

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

eax

-12 bytes

165

3F03 - Compiler Outputpush eax

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptr

166Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptr

ecx-8 bytes

3F03 - Compiler Outputmov ecx,dword ptr _b$[ebp]

167

3F03 - Compiler Outputpush ecx

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7

ecx

168Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7

ecxedx

-4

3F03 - Compiler Outputmov edx,dword ptr _a$[ebp]

169

3F03 - Compiler Outputpush edx

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

edxecx

170

3F03 - Compiler Outputpush ebp

now in sums

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main)

edxecx

171

3F03 - Compiler Outputmov ebp,esp

Stack frame

ebp ebp

esp

76 bytes

ebxesiedi

... ...11001100110011001100110011001100

... ...

11001100110011001100110011001100

11001100110011001100110011001100

57

ptr

eax = ptrecx = 7edx = 5

ebp (main) ebp

edxecx

172

3F03 - Compiler Outputsub 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

173

3F03 - Compiler Outputpush 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

174

3F03 - Compiler Outputpush 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

175

3F03 - Compiler Outputpush 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

176

3F03 - Compiler Outputlea 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

177Stack 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 Outputmov eax,dword ptr _a$[ebp]

_a$ = 4 in sums

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+eax

edxecx

8 bytes

3F03 - Compiler Outputadd eax,dword ptr _b$[ebp]

_b$ = 8 in sums

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+eaxecx

edxecx

12 bytes

3F03 - Compiler Outputmov ecx,dword ptr _c$[ebp]

_c$ = 12 in sums

180

3F03 - Compiler Outputmov 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

181

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 parametersare placed on the stack in the order from right to left

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

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

182

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));

}

183

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

184

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

pushebpmov ebp, espsub esp, 72 ; 00000048Hpushebxpushesipushedilea 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]pusheaxmov ecx, DWORD PTR _a$[ebp]pushecxcall_sums

185

3F03 - Compiler Output

add esp, 8pusheaxpushOFFSET 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

186

3F03 - Compiler Output

• What about c functions that return values?The parameters are handled as previouslynoted.Scalar returned values are returned in AX orDX:AX.

187

3F03 - Compiler Output

• How about gcc?In case we get the idea that the interfacedetails we observe using Visual C++ arerestricted to that implementation (it should notbe), we can also examine output from gcc.To this end, we can examine the assemblerlisting generated for TestSums.c by gcc.The listing is shown next.

188

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

189

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

190

3F03 - Compiler Output

• So, gcc interfaces work in exactly the sameway as do the interfaces in Visual C++

The gcc compiler does not fill the stack framebetween ebp and esp with a known value asdoes Visual C++

191

3F03 - Inline Assembler

• Inline assemblerMany high-level language compilers allow us towrite blocks of assembler code within a sub-programThe advantage of inline assembler is that theinterfacing between programs is handled by thehigh-level languageThe assembler instructions available to be usedinline usually forms a subset of the full set ofinstructions available to be used if we arewriting the entire sub-program in assemblerExamples in Visual C++ and gcc follow -

192

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 { }

193

3F03 - Inline Assembler

• The previous example does not show whyinline assembler is useful, since we usedour knowledge of how the interface works toaccess the parameters.

• This was done to demonstrate the actualinline process.

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

194

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!

195

3F03 - Inline Assembler

• gcc also allows inline assembler as shownon the next slide.

196

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, etcregisters need %% prefix

first input is a register

197

3F03 - Inline Assembler

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

environment that implements an objectPascal compiler and supports inlineassembler.

198

3F03 - Inline Assembler

199

3F03 - Inline Assembler

200

3F03 - Inline Assembler

201

3F03 - Inline Assembler

202

3F03 - Interfacing Assembler

• Don’t assume that all languages have thesame rules for setting up the interfacebetween procedures.

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

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

sometimes different implementations of thesame language) have different rules forwhich registers must be protected.

203

3F03 - Interfacing Assembler

• Calling assembler sub-programs from ahigh-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

204

3F03 - 80x86 Conclusions

• Final commentsNote that a major difference between manyhigh-level languages and assembler is that theassembler does not support type checking.We have not considered floating-pointoperations.We have not discussed macros which are amajor contribution to productivity in assemblerprogramming.We have concentrated on principles andconcepts.

205

3F03 - Introduction to MIPS

• MIPS32 registers - prefixed by $“simple” instructionsmany 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

206

3F03 - Introduction to MIPS

• MIPSRegister type addressing mode

• all operands are registersImmediate type addressing mode

• one of the operands is a constantJump type addressing mode

• pseudoindirect - 26 bit address fieldBase or displacement addressing mode

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

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

207

3F03 - Introduction to MIPS

• MIPSInstruction categories

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

• Data transferload word (lw), store word (sw), store byte (sb), load upperimmediate (lui), copy register to register (move)

• Conditional branchbranch on equal (beq), branch on not equal (bne), set onless than (slt), set less than immediate (slti)

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

208

3F03 - Introduction to MIPS

• Simple example.text

main: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

209

3F03 - Introduction to MIPS

• We can use SPIM to simulate the MIPShardware. SPIM is available for the majoroperating systems. We will use the PCversion.

210

3F03 - Introduction to MIPS

211

3F03 - Introduction to MIPS

• System Callsload system call code into $v0load arguments into $a0-$a3values returned in $v0 (if necessary)

see Patterson & Hennessy, pages A-48/49

212

3F03 - Introduction to MIPS

• Back to the example.data

str:.asciiz “Result is: “.text

main: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

213

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 registert5sw $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

214

3F03 - Introduction to MIPS

• So what does the previous example do?We can re-comment the example, being morespecific about the effect of each statement

215

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

216

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

217

3F03 - Introduction to MIPS

• MIPSMIPS is a load-store architecture

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

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

Well-suited for use by compilers rather than byhuman coders