Download presentation
Presentation is loading. Please wait.
Published byDaniella Warner Modified over 8 years ago
1
Machine Language - 3F03 Alan Wassyng ITB 166 wassyng@mcmaster.ca January - April 2006
2
1 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);
3
2 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
4
3 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
5
4 3F03 - Overview Required Steps So, 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
6
5 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.
7
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
8
7 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.
9
8 3F03 - Overview Course Rules TAs Yazhi Wang - wangy58@mcmaster.ca Ryan Lortie - desrt@cas.mcmaster.ca 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.
10
9 3F03 - Overview Course Rules Lectures Tue,Wed,Fri 9:30 ABB 271 Tutorial Thur 9:30 ITB 237 http://www.cas.mcmaster.ca/~se3f03/
11
10 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
12
11 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.
13
12 3F03 - Overview Table of Contents Assembler basics Computer arithmetic, binary, octal, hex 80x86 assembler through example problems - Debug & NASM BIOS and Operating Systems Simple data structures in assembler Understanding compiler output Inline assembler Interfacing to high-level languages MIPS - Using SPIM CISC versus RISC hardware 13 20 22 75 112 146 190 201 204 216 Page
14
13 3F03 - Assembler Basics Machine Language A typical processor Registers are simply fast memory locations. Some may have specific roles to play, while others may be more general.
15
14 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.
16
15 3F03 - Assembler Basics Machine Instructions Each processor has its own unique set of registers and instructions. Examples: 10110100 00001001moves 09 into high byte of reg AX 01000011increment reg BX by 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.)
17
16 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.
18
17 3F03 - Assembler Basics 80x86 assembler (Intel notation) General structure labelmnemonic operand(s)comment SET:MOV AX,BX;move BX into AX dest,source ADD AX,BX;add BX to AX and ;store in AX
19
18 3F03 - Assembler Basics 80x86 Layout
20
19 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
21
20 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
22
21 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
23
22 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.
24
23 3F03 - 80x86 assembler Addresses Consider the address 0001:0032
25
24 3F03 - 80x86 assembler Open a DOS window and run Debug.
26
25 3F03 - 80x86 assembler A few Debug commands -aassemble following statements end by blank statement -uunassemble -u addrunassemble from addr(ess) -ttrace, execute next statement and show registers -rshow registers -?get list of commands -qquit
27
26 3F03 - 80x86 assembler Debug examples
28
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 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
29
28 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)
30
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?moval, F2 mov ah, 12 movbl, 0F mov bh, 22 addal, bl addah, bh
31
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 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
32
31 3F03 - 80x86 assembler If we use ADC rather than ADD everything works just fine. moval, F2 mov ah, 12 movbl, 0F mov bh, 22 addal, bl adcah, bh
33
32 3F03 - 80x86 assembler URL for Intel 80x86 Instructions http://www.penguin.cz/~literakl/intel/intel.html
34
33 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
35
34 3F03 - 80x86 assembler Loop example - in Debug 1478:0100MOV AX,0 1478:0103MOV CX,5 1478:0106INC AX 1478:0107LOOP 0106
36
35 3F03 - 80x86 assembler Loop example - in Debug 1478:0100MOV AX,0 1478:0103MOV CX,5 1478:0106INC AX 1478:0107LOOP 0106 counter
37
36 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
38
37 3F03 - 80x86 assembler Loop example - in Assembler MOV AX,0 MOV CX,5 LBL:INC AX LOOP LBL
39
38 3F03 - 80x86 assembler Loop example - in Assembler MOV AX,0 MOV CX,5 LBL:INC AX LOOP LBL
40
39 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
41
40 3F03 - 80x86 assembler Loop example - assemble source Assemble and link file using NASM NASM TestLoop.asm -o TestLoop.com Assemble, link & make listing file NASM TestLoop.asm -l TestLoop.txt -o TestLoop.com
42
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
43
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 number calculated?
44
43 3F03 - 80x86 assembler Loop, with CMP and Jxx - in Debug 1478:0100MOV AX,0 1478:0103MOV CX,5 1478:0106INC AX 1478:0107DEC CX 1478:0108CMP CX,0 1478:010BJNZ 0106
45
44 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.
46
45 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 … …...
47
46 3F03 - 80x86 assembler Loop, with CMP and Jxx - in Debug 1478:0100MOV AX,0 1478:0103MOV CX,5 1478:0106INC AX 1478:0107DEC CX 1478:0108JNZ 0106
48
47 3F03 - 80x86 assembler Typical addressing modes labelopcodeoperands 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
49
48 3F03 - 80x86 assembler Typical addressing modes Register and immediate modes we have already seen MOVAX,1 MOVBX,AX register immediate
50
49 3F03 - 80x86 assembler Typical addressing modes Absolute address mode MOVAX,[0200] value stored in memory location DS:0200
51
50 3F03 - 80x86 assembler Typical addressing modes Register indirect MOVAX,[BX] value stored at address contained in DS:BX
52
51 3F03 - 80x86 assembler Typical addressing modes Displacement MOVDI,4 MOVAX,[0200+DI] value stored at DS:0204
53
52 3F03 - 80x86 assembler Typical addressing modes Indexed MOVBX,0200 MOVDI,4 MOVAX,[BX+DI] value stored at DS:0204
54
53 3F03 - 80x86 assembler Typical addressing modes Memory indirect MOVDI,0204 MOVBX,[DI] MOVAX,[BX] If DS:0204 contains 0256, then AX will contain whatever is stored at DS:0256
55
54 3F03 - 80x86 assembler Typical addressing modes Memory indirect MOVDI,0204 MOVBX,[DI] MOVAX,[BX] If DS:0204 contains 0256, then AX will contain whatever is stored at DS:0256
56
55 3F03 - 80x86 assembler 80x86 Restrictions MOV cannot move memory to memory MOV AX,[0200]Use this instead MOV [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.
57
56 3F03 - 80x86 assembler Complications arising from memory access instructions ConsiderNEG [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]
58
57 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 ….
59
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, "$"
60
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, "$"
61
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 Section START: mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino di COPY: 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 "$"
62
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 Section START: mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino di COPY: 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 "$"
63
62 3F03 - 80x86 assembler ; TextEx5.asm [BITS 16] ; Set 16 bit code [ORG 0100H] ; Set code start address to 100h (COM file) ; Set constants CR equ 0DH LF equ 0AH EOS equ "$" [SECTION.text] ; Code Section START: mov si, msg ; Loads ADDRESS of msg into si mov di, msg2 ; Loads ADDRESS of msg2 ino di COPY: 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, EOS msg2 times 80 db EOS
64
63 3F03 - 80x86 assembler ; TextEx6.asm [BITS 16] ; Set 16 bit code [ORG 0100H] ; Set code start address to 100h (COM file) ; Set constants CR equ 0DH LF equ 0AH EOS equ "$" [SECTION.text] ; Code Section START: 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 di COPY: 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, EOS msglen db $-msg msg2 times 80 db EOS
65
64 3F03 - 80x86 assembler ; TextEx6f.asm [BITS 16] ; Set 16 bit code [ORG 0100H] ; Set code start address to 100h (COM file) ; Set constants CR equ 0DH LF equ 0AH EOS equ "$" [SECTION.text] ; Code Section START: 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 di COPY: 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, EOS msglen db $-msg msg2 times 80 db EOS
66
65 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.
67
66 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.
68
67 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.
69
68 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.
70
69 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.
71
70 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.
72
71 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.
73
72 3F03 - 80x86 assembler Example of an EXE file ; TestExe.asm segment 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,0005 LBL: inc ax loop LBL mov ax, $4C00 ; Prepare to exit int 0x21 ; Terminate prog segment data PUBLIC segment stack stack resb 64 stacktop:
74
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
75
74 3F03 - 80x86 assembler ; TestExe2.asm segment code PUBLIC..start: mov ax,data mov ds,ax mov ax,stack mov ss,ax mov sp,stacktop mov cx,0005 LBL: 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 PUBLIC msg db "This is just an example",CR,LF,EOS CR equ 0x0D LF equ 0x0A EOS equ "$" segment stack stack resb 64 stacktop:
76
75 3F03 - 80x86 assembler ; TestExe3.asm segment 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 PUBLIC msg db "This is just an example",CR,LF,EOS CR equ 0x0D LF equ 0x0A EOS equ "$" segment stack stack resb 64 stacktop:
77
76 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.
78
77 3F03 - 80x86 assembler Device Drivers
79
78 3F03 - 80x86 assembler Example - Graphics through BIOS Set video modes (INT 10h, Fn 0) AH = 0, AL = video mode Typical video modes 280 x 252 colours text 380 x 2516 colourstext 0Dh320 x 20016 coloursgraphics 12h640 x 480 16 coloursgraphics 13h320 x 200256 coloursgraphics 6Ah800 x 60016 coloursgraphics Coordinates 0,0 top leftXmax,Ymax bottom right
80
79 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
81
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 be XORed with the current contents of the pixel. This allows us to erase pixels.
82
81 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
83
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 Section START: 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,0CH Line: 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 data savemode db 0
84
83 3F03 - 80x86 assembler ; DrawLn2.asm START: 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,191 Yloop: dec dx mov cx,9 Line: 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 data savemode db 0 x y
85
84 3F03 - 80x86 assembler ; DrawLn3.asm START: 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,191 Yloop: dec dx mov cx,9 mov al,3 Line: 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 data savemode db 0 Pixel colour
86
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,191 Yloop: dec dx mov cx,9 xor al,al Zero AL Line: inc al Next colour cmp al,15 jl NoReset xor al,al Zero AL if colour > 15 - starts from 0 again NoReset: 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 data savemode db 0
87
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,191 Yloop: dec dx mov cx,9 xor al,alZero AL Line: 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 data savemode db 0
88
87 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
89
88 3F03 - 80x86 assembler Memory-Mapped Graphics
90
89 3F03 - 80x86 assembler ; MemMap1.asm segment code..start: mov ax,dataHave to do it as an EXE program mov ds,axsince we need access to memory mov ax,stackoutside 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
91
90 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
92
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 data savemode resb 1 segment stack stack resb 256 stacktop:
93
92 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
94
93 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:
95
94 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.
96
95 3F03 - 80x86 assembler Screen display from DrawLn Screen display from MemMap
97
96 3F03 - 80x86 assembler Screen display from DrawLn Screen display from MemMap Single pixel “blip” caused by pressing PrtSc
98
97 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)
99
98 3F03 - 80x86 assembler ; MemMap4.asm segment 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 data savemode resb 1 segment stack stack resb 256 stacktop:
100
99 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: ColourRed GreenBlue i=1,63 i 0 0 63+i i 15 15 126+i i 30 30 189+i i 45 45 253,255No Change to these
101
100 3F03 - 80x86 assembler ; MemMap5.asm segment 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
102
101 3F03 - 80x86 assembler OuterLoop: mov cx,63 ; 63 counts in inner loop InnerLoop: 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 data savemode resb 1 segment stack stack resb 256 stacktop:
103
102 3F03 - 80x86 assembler Output of original palette colours Output of modified palette colours Single pixel “blip” caused by pressing PrtSc
104
103 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.
105
104 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.
106
105 3F03 - 80x86 assembler Setting the colour of a single pixel - 16 colours
107
106 3F03 - 80x86 assembler Setting the colour of a single pixel - 16 colours
108
107 3F03 - 80x86 assembler Memory map - 16 colours
109
108 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 b n... b 3 b 2 b 1 b 0, 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 2 n Shift left SHL AX,n is equivalent to multiplication by 2 n
110
109 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.
111
110 3F03 - 80x86 assembler 16 colour bit planes A0000
112
111 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.)
113
112 3F03 - 80x86 assembler Hardware ports We already saw that we can output values to a port: MOVDX,port number MOVAL,byte to output to port OUTDX,AL Similarly, we can read inputs from a port: MOVDX,port number INAL,DX We can read/write bytes/words and sometimes double words from/to ports. Can also use “immediate” port numbers.
114
113 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.
115
114 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: array1db20H, 30H, 40H array2dw0251H, 4521H, 3F61H array3times 200db0 array4resw150
116
115 3F03 - Data structures in assembler Arrays Size of the array Could manually count elements (not practical) array1db23H, 41H, 55H, 26H array1_szdb4 Use “current location” as we did with strings array2db23H, 41H, 55H, 26H array2_szequ$-array2 array3dw1234H, 2F43H, 3AE4H, 6A7BH array3_szequ($-array3)/2 Size is an integral part of the definition array4_szequ250 array4resbarray4_sz
117
116 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.)
118
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 array Init: 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 index Sum: 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 data IntArray times 80 dw 0
119
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 data IntArray times 80 dw 0 result db '0000',CR,LF,EOS digits db '0123456789ABCDEF'
120
119 3F03 - Data structures in assembler
121
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 Section CR equ 0DH LF equ 0AH EOS 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 data screenstr times 1600 db '-' db CR,LF,EOS
122
121 3F03 - Data structures in assembler
123
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 Section CR equ 0DH LF equ 0AH EOS 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 data screenstr times 1600 db '-' db CR,LF,EOS
124
123 3F03 - Data structures in assembler
125
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 0DH LF equ 0AH EOS 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 data screenstr times 1600 db '-' db CR,LF,EOS Modified
126
125 3F03 - Data structures in assembler
127
126 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.
128
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
129
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 data CR equ 0DH LF equ 0AH EOS 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, 53H Nrows equ 4 Ncols equ 5 sum dw 0 result db '0000',CR,LF,EOS digits db '0123456789ABCDEF'
130
129 3F03 - Data structures in assembler
131
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 Section START: ; 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..........
132
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 Section START: 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 data CR equ 0DH LF equ 0AH EOS 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, 53H Nrows equ 4 Ncols equ 5 sum times Ncols dw 0 result db '0000',CR,LF,EOS digits db '0123456789ABCDEF'
133
132 3F03 - Data structures in assembler
134
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 Section START: xor di,di ; zero col index mov bp,sum ; bp points to sum NextCol: 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 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,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
135
134 3F03 - Data structures in assembler ; display result mov bp,[bp+di] ; store column total 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 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 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 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
136
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 data CR equ 0DH LF equ 0AH EOS 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, 53H Nrows equ 4 Ncols equ 5 sum times Ncols dw 0 result db '0000',CR,LF,EOS digits db '0123456789ABCDEF'
137
136 3F03 - Data structures in assembler So, what was wrong with our program?
138
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 afterwards NextRow:.... 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 0 result db '0000',CR,LF,EOS digits db '0123456789ABCDEF'
139
138 3F03 - Data structures in assembler
140
139 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.
141
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 Section START: xor di,di ; zero col index NextCol: 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 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,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
142
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 col NoMore: 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, 53H Nrows equ 4 Ncols equ 5 sum times Ncols dw 0 result db '0000',CR,LF,EOS digits db '0123456789ABCDEF'
143
142 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”.
144
143 3F03 - Data structures in assembler Struc example Employee info struc employee.name:resb32; string[32].salary:resd1; 32-bit integer.date:resb3; yy mm dd endstruc Create an instance of employee by person:istruc employee at employee.name db ‘A.N. Other’ at employee.salary dd 75000 at employee.date db 01H, 0BH, 1AH iend
145
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 Section CR equ 0DH LF equ 0AH EOS 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
146
145 3F03 - Data structures in assembler
147
146 3F03 - Data structures in assembler ; Struc2.asm.... START: struc employee.name: resb 32.salary: resd 1.date: resb 3 endstruc person1: 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 output person2: 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
148
147 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
149
148 3F03 - Compiler Output /* TestSums.c */ #include 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); }
150
149 3F03 - Compiler Output Output from Visual C++ PUBLIC_sums ;COMDAT _sums _TEXTSEGMENT _a$ = 8 _b$ = 12 _c$ = 16
151
150 3F03 - Compiler Output _sumsPROC NEAR; COMDAT ; File C:\3f03_2002\Asm\TestSums.c ; Line 7 pushebp movebp, esp subesp, 64; 00000040H pushebx pushesi pushedi leaedi, DWORD PTR [ebp-64] movecx, 16; 00000010H moveax, -858993460; ccccccccH rep stosd ; Line 8 moveax, DWORD PTR _a$[ebp] addeax, DWORD PTR _b$[ebp] movecx, DWORD PTR _c$[ebp] movDWORD PTR [ecx], eax ; Line 9 popedi popesi popebx movesp, ebp popebp ret0 _sumsENDP _TEXTENDS
152
151 3F03 - Compiler Output PUBLIC_main PUBLIC??_C@_07JNMI@sum?$DN?$CFd?6?$AA@; `string' EXTRN_printf:NEAR EXTRN__chkesp:NEAR ;COMDAT ??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ ; File C:\3f03_2002\Asm\TestSums.c CONSTSEGMENT ??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ DB 'sum=%d', 0aH, 00H ; `string' CONSTENDS ;COMDAT _main _TEXTSEGMENT _a$ = -4 _b$ = -8 _c$ = -12
153
152 3F03 - Compiler Output _mainPROC NEAR; COMDAT ; File C:\3f03_2002\Asm\TestSums.c ; Line 13 pushebp movebp, esp subesp, 76; 0000004cH pushebx pushesi pushedi leaedi, DWORD PTR [ebp-76] movecx, 19; 00000013H moveax, -858993460; ccccccccH rep stosd ; Line 15 movDWORD PTR _a$[ebp], 5 ; Line 16 movDWORD PTR _b$[ebp], 7 ; Line 17 leaeax, DWORD PTR _c$[ebp] pusheax movecx, DWORD PTR _b$[ebp] pushecx movedx, DWORD PTR _a$[ebp] pushedx call_sums addesp, 12; 0000000cH
154
153 3F03 - Compiler Output ; Line 18 moveax, DWORD PTR _c$[ebp] pusheax pushOFFSET FLAT:??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ ; `string' call_printf addesp, 8 ; Line 20 popedi popesi popebx addesp, 76; 0000004cH cmpebp, esp call__chkesp movesp, ebp popebp ret0 _mainENDP _TEXTENDS
155
154 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
156
155 3F03 - Compiler Output push ebp
157
156 3F03 - Compiler Output mov ebp,esp
158
157 3F03 - Compiler Output sub esp,76
159
158 3F03 - Compiler Output push ebx
160
159 3F03 - Compiler Output push esi
161
160 3F03 - Compiler Output push edi
162
161 3F03 - Compiler Output lea edi, dword ptr [ebp-76] mov ecx,19 mov eax,-858993460 rep stosd
163
162 3F03 - Compiler Output mov dword ptr _a$[ebp],5 _a$ = -4 in main
164
163 3F03 - Compiler Output mov dword ptr _b$[ebp],7 _b$ = -8 in main
165
164 3F03 - Compiler Output lea eax,dword ptr _c$[ebp] _c$ = -12 in main
166
165 3F03 - Compiler Output push eax
167
166 3F03 - Compiler Output mov ecx,dword ptr _b$[ebp]
168
167 3F03 - Compiler Output push ecx
169
168 3F03 - Compiler Output mov edx,dword ptr _a$[ebp]
170
169 3F03 - Compiler Output push edx
171
170 3F03 - Compiler Output push ebp now in sums
172
171 3F03 - Compiler Output mov ebp,esp
173
172 3F03 - Compiler Output sub esp,64
174
173 3F03 - Compiler Output push ebx
175
174 3F03 - Compiler Output push esi
176
175 3F03 - Compiler Output push edi
177
176 3F03 - Compiler Output lea edi,dword ptr [ebp-64] mov ecx,16 mov eax, -858993460 rep stosd
178
177 3F03 - Compiler Output mov eax,dword ptr _a$[ebp] _a$ = 4 in sums
179
178 3F03 - Compiler Output add eax,dword ptr _b$[ebp] _b$ = 8 in sums
180
179 3F03 - Compiler Output mov ecx,dword ptr _c$[ebp] _c$ = 12 in sums
181
180 3F03 - Compiler Output mov dword ptr [ecx],eax
182
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 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
183
182 3F03 - Compiler Output /* TestSum2.c */ #include 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)); }
184
183 3F03 - Compiler Output _TEXTSEGMENT _a$ = 8 _b$ = 12 _sumsPROC NEAR; COMDAT ; File C:\3F03_2002\ASM\TestSum2.c ; Line 7 pushebp movebp, esp subesp, 64; 00000040H pushebx pushesi pushedi leaedi, DWORD PTR [ebp-64] movecx, 16; 00000010H moveax, -858993460; ccccccccH rep stosd ; Line 8 moveax, DWORD PTR _a$[ebp] addeax, DWORD PTR _b$[ebp] ; Line 9 popedi popesi popebx movesp, ebp popebp ret0 _sumsENDP _TEXTENDS
185
184 3F03 - Compiler Output ;COMDAT _main _TEXTSEGMENT _a$ = -4 _b$ = -8 _mainPROC NEAR; COMDAT ; File C:\3F03_2002\ASM\TestSum2.c ; Line 13 pushebp movebp, esp subesp, 72; 00000048H pushebx pushesi pushedi leaedi, DWORD PTR [ebp-72] movecx, 18; 00000012H moveax, -858993460; ccccccccH rep stosd ; Line 15 movDWORD PTR _a$[ebp], 5 ; Line 16 movDWORD PTR _b$[ebp], 7 ; Line 17 moveax, DWORD PTR _b$[ebp] pusheax movecx, DWORD PTR _a$[ebp] pushecx call_sums
186
185 3F03 - Compiler Output addesp, 8 pusheax pushOFFSET FLAT:??_C@_07JNMI@sum?$DN?$CFd?6?$AA@ ; `string' call_printf addesp, 8 ; Line 19 popedi popesi popebx addesp, 72; 00000048H cmpebp, esp call__chkesp movesp, ebp popebp ret0 _mainENDP _TEXTENDS END
187
186 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.
188
187 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.
189
188 3F03 - Compiler Output.file"TestSums.c" gcc2_compiled.: ___gnu_compiled_c:.text.p2align 2.globl _sums _sums: pushl %ebp movl %esp,%ebp pushl %ebx movl 16(%ebp),%eax movl 8(%ebp),%edx movl 12(%ebp),%ecx leal (%ecx,%edx),%ebx movl %ebx,(%eax) L1: movl -4(%ebp),%ebx leave ret LC0:.ascii "sum=%d\12\0".p2align 2 gcc uses AT&T assembler format for the generated assembler. inssource,destination ins will have b, w, or l as a suffix: b - byte 8 bits w - word16 bits l - long32 bits % precedes registers n(%eax) means %eax+n
190
189 3F03 - Compiler Output.globl _main _main: pushl %ebp movl %esp,%ebp subl $12,%esp movl $5,-4(%ebp) movl $7,-8(%ebp) leal -12(%ebp),%eax pushl %eax movl -8(%ebp),%eax pushl %eax movl -4(%ebp),%eax pushl %eax call _sums addl $12,%esp movl -12(%ebp),%eax pushl %eax pushl $LC0 call _printf addl $8,%esp L2: leave ret clean up stack etc
191
190 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++
192
191 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 -
193
192 3F03 - Inline Assembler /* TestSum3.c - Visual C++ */ #include int sums(int a, int b); int sums(int a, int b) { int r; _asm { moveax, DWORD PTR 8[ebp] addeax, 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 inline assembler block enclosed within { }
194
193 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:
195
194 3F03 - Inline Assembler /* TestSum5.c - Visual C++ */ #include int sums(int a, int b); int sums(int a, int b) { int r; _asm { moveax, a addeax, b movr, eax } return (r); } main() { int a,b; a = 5; b = 7; printf("sum=%d\n",sums(a,b)); } In an inline assembler block we can directly reference existing identifiers!
196
195 3F03 - Inline Assembler gcc also allows inline assembler as shown on the next slide.
197
196 3F03 - Inline Assembler /* TstSum4.c - gcc */ #include 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
198
197 3F03 - Inline Assembler What about other languages? For example, Delphi is an integrated environment that implements an object Pascal compiler and supports inline assembler.
199
198 3F03 - Inline Assembler
200
199 3F03 - Inline Assembler
201
200 3F03 - Inline Assembler
202
201 3F03 - Inline Assembler
203
202 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.
204
203 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
205
204 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.
206
205 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
207
206 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
208
207 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)
209
208 3F03 - Introduction to MIPS Simple example.text main: li$s0, 62# f li$s1, 55# g li$s2, 7# h li$s3, 96# k add $t0, $s0, $s1# f + g add$t1, $s2, $s3# h + k sub$s0, $t0, $t1# (f + g) - (h + k) jr$ra# unconditional jump # to return address
210
209 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.
211
210 3F03 - Introduction to MIPS
212
211 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
213
212 3F03 - Introduction to MIPS Back to the example.data str:.asciiz “Result is: “.text main: li$s0, 62# f li$s1, 55# g li$s2, 7# h li$s3, 96# k add $t0, $s0, $s1# f + g add$t1, $s2, $s3# h + k sub$s0, $t0, $t1# (f + g) - (h + k) li$v0, 4# for print_str la$a0, str# address of str syscall# print the string li$v0, 1# for print_int move$a0, $s0# put result in $a0 syscall# print result jr$ra# unconditional jump # to return address
214
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 program li$t6, 1# load imediate value 1 into register t6 li$t7, 4# load immediate value 4 into register t7 sw$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:# label addi$t3, $t0, -8# add immediate value -8 to value in register t0 # and store in register t3 addi$t4, $t0, -4# add immediate value -4 to value in register t0 # and store in register t4 lw$t1, theArray($t3)# load word from theArray[0+t3] in register t1 lw$t2, theArray($t4)# load word from theArray[0+t4] in register t2 add$t5, $t1, $t2# add values in registers t1 and t2 in register t5 sw$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 t0 blt$t0, 160, loop# branch to loop if value in register t0 < 160 jr$ra# unconditionally jump to address in register ra
215
214 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
216
215 3F03 - Introduction to MIPS Specific annotation for 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 program li$t6, 1# load imediate value 1 into register t6 # initial value = 1 li$t7, 4# load immediate value 4 into register t7 sw$t6, theArray($0)# store word from register t6 in theArray[0] # f(0) = 1 sw$t6, theArray($t7)# store word from register t6 in theArray[0+t7] # f(1) = 1 li$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
217
216 3F03 - Introduction to MIPS So what does it do? It computes and stores the Fibonacci numbers: fib(0) = 1 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2), n=2,3,4,…,39
218
217 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.