Top Banner
Chapter 4 H1 Assembly Language: Part 2
117

Chapter 4

Jan 15, 2016

Download

Documents

quilla

H1 Assembly Language: Part 2. Chapter 4. Contains the absolute address of the memory location it accesses. ld instruction: 0000 000000000100. Direct instruction. Absolute address. Shorthand notation for ld x ac = mem[x]; where 0
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 4

Chapter 4

H1 Assembly Language: Part 2

Page 2: Chapter 4

Direct instructionContains the absolute address of the memory location it accesses.

ld instruction:

0000 000000000100

Absolute address

Page 3: Chapter 4

Shorthand notation for ld x

ac = mem[x];

where

0 <= x <= 4095

Page 4: Chapter 4
Page 5: Chapter 4

code

stacksp

stack frame for main()missing in H1

Page 6: Chapter 4

Stack instructions

• push pushes the ac register contents onto the top of the stack (decrement sp first).

• pop removes the value of top of the stack and loads it into the ac register (inc sp after).

• swap exchanges the values in the ac and sp registers (don't modify sp).

• sp register points to the top of the stack.• sp is predecremented on a push.

Page 7: Chapter 4

Prof DosReis uses register f (15)

Page 8: Chapter 4
Page 9: Chapter 4

Immediate instructions

An immediate instruction contains the operand—not the operand address as in the direct instructions. Because it is in the instruction, the operand is “immediately” available.

Page 10: Chapter 4
Page 11: Chapter 4

ldc 1

Machine code:

1000 000000000001

Loads 1 (the operand in the instruction itself) into the ac register (zero extends operand to 16 bits).

Page 12: Chapter 4

Execution of ldc 1

sign

Page 13: Chapter 4

What does this program do?

Page 14: Chapter 4

What an assembler does• Translates mnemonics to binary opcodes.

• Translate labels to binary addresses.

• Translates numbers to binary.

• Translates strings to ASCII codes.

ld x 0000 0000 0000 0100

ldc x 1000 0000 0000 0100

ldc 5 1000 0000 0000 0101

dw ‘A’ 0000000001000001

Page 15: Chapter 4

ldc w

This instruction loads the address of w into the ac register.

1000 000000001111

Address of w

Page 16: Chapter 4

0

1

2

3

4

5

6

7

8

9

A

Page 17: Chapter 4

ldc ‘A’

This instruction loads the ASCII code for ‘A’ into the ac register.

1000 000001000001

ASCII code for ‘A’

Page 18: Chapter 4
Page 19: Chapter 4

Uses of the ldc instruction

Page 20: Chapter 4

aloc and dloc instructions

• aloc 2 subtracts 2 from sp register, reserving two slots on the stack

• dloc 2 adds 2 to the sp register, deallocating two slots on the stack.

Page 21: Chapter 4

Effect of aloc 2

Page 22: Chapter 4
Page 23: Chapter 4
Page 24: Chapter 4

Running sim without the debugger

Page 25: Chapter 4

dout, hout, aout do not output newlines

Output: 650041A

Page 26: Chapter 4

Output of previous program is

Page 27: Chapter 4

To output a newline character, use the aout instruction:

ldc ‘\n’

aout

Page 28: Chapter 4
Page 29: Chapter 4

When an input instruction is executed, the system waits until the user enters the required input on the keyboard.

Page 30: Chapter 4

Process State Diagram:

start

stop

wait

run

IO blocking

Page 31: Chapter 4
Page 32: Chapter 4

sin and sout

• sin and sout, respectively, input and output to the memory location pointed to by the ac register.

• sout continues until a null character is reached and outputed.

• Be sure to use double-quoted strings with sout (because they are null terminated).

Page 33: Chapter 4
Page 34: Chapter 4
Page 35: Chapter 4

How does a jump instruction work?

Machine code ja 1 9001 The execution of this instruction loads 1 into the pc register.

Page 36: Chapter 4

ldc 5 jz xxx ; no jump

ldc 5 jnz yyy ; jump occurs

Conditional Jump Instructions

Page 37: Chapter 4

Infinite loop

Page 38: Chapter 4

Sim’s response to infinite loop

Page 39: Chapter 4

Count-controlled loop

A loop whose number of iterations depends on a counter.

The program on the next slide computes 20 + 19 + …+ 1 using a count-controlled loop..

Page 40: Chapter 4

7 instructionsinside loop

Page 41: Chapter 4

Loop With Jump to Test:start: ja testloop: ld sum

add countst sum

test: ld countsub @1st countjnz loop

done: ldc msgsoutld sumdoutldc '\n'aouthaltend start

; datacount: dw 21msg: dw “sum = “sum: dw 0@1: dw 1

Original Problem: Add thefirst twenty positive integers

Programming Logic Problem:Counter starts at 21 so you mightthink we'll end up adding the firsttwenty-one positive integers

Note: 7 instructions inside loop

Page 42: Chapter 4

Loop With Jump to Test Rewrite: start: ja testloop: ld sum

add countst sumld countsub @1st count

test: ld countjnz loop

done: ldc msgsoutld sumdoutldc '\n'aouthaltend start

; datacount: dw 20msg: dw “sum = “sum: dw 0@1 dw 1

Original Problem: Add thefirst twenty positive integers

Programming Logic Problem:Gone since the count variablenow decreases from 20 to 0

Note: 8 instructions inside loopbut ld count immediately followedby ld sum; unnecessary

Page 43: Chapter 4

Loop With Jump to Test Rewrite: start: ja testloop: add sum

st sumld countsub @1st count

test: ld countjnz loop

done: ldc msgsoutld sumdoutldc '\n'aouthaltend start

; datacount: dw 20msg: dw “sum = “sum: dw 0@1 dw 1

Original Problem: Add thefirst twenty positive integers

Programming Logic Problem:Gone since the count variablenow decreases from 20 to 0

Note: 7 instructions inside loopad ld count immediately followedby add sum; more efficient

Page 44: Chapter 4

Loop With Jump to Test Rewrite: ld countstart: ja testloop: add sum

st sumld countsub @1st count

test: jnz loopdone: ldc msg

soutld sumdoutldc '\n'aouthaltend start

; datacount: dw 20msg: dw “sum = “sum: dw 0@1 dw 1

Original Problem: Add thefirst twenty positive integers

Programming Logic Problem:Gone since the count variablenow decreases from 20 to 0

Note: 6 instructions inside loopad ac == count immediately followedby add sum; more efficient

Page 45: Chapter 4

Indirect instruction

Accesses the memory location pointed to by the ac register.

Used to dereference pointers.

Page 46: Chapter 4

ac == addressac == value

NOTE: ldi only needs one thing to start – address – and thenit gets the value. However, sti is mem --> mem so needs twoaddresses to start but only has one register (ac)

pops thestackautomatically

Page 47: Chapter 4

ldi and sti instructions

• ldi loads the ac register from the memory location pointed to by the ac register.

• sti pops the stack and stores the popped value at the memory location pointed to by the ac register.

• So we use the stack to store the second address

Page 48: Chapter 4

Assembler code forx = *p;

ld p ; p contains an address

ldi

st x

In C or C++, *p means “the valuepointed at by p” and we say we are“dereferencing p”

In Java, if p is a “reference” then a “value pointed at by p” might be p.x.

Page 49: Chapter 4

x = *p

Page 50: Chapter 4

Java Example:

• Assembler code for

class P { public int : x,y};int z;...P p = newP();...z = p.y;

z = p.y;

@2 dw 2

ld padd @2 ; integer is 2 bytesldist z

Page 51: Chapter 4

Assembler code for*p = 5;

ldc 5

push

ld p

sti

Page 52: Chapter 4
Page 53: Chapter 4
Page 54: Chapter 4

Java Example:

• Assembler code for

class P { public int : x,y};int z;...P p = newP();...p.x = p.y;

p.x = p.y;

@2 dw 2

ld padd @2ldipushld psti

Page 55: Chapter 4

A direct instruction holds an absolute address (i.e., its rightmost 12 bits).

An immediate instruction holds an operand.

A relative instruction holds arelative address.

A relative address is an address relative to the location to which sp points.

Page 56: Chapter 4

location sp+x inside the stack

Page 57: Chapter 4

What do these instructions do?

Machine code ld 2 0002 ldc 2 8002 ldr 2 4002

x field

Page 58: Chapter 4

Assume sp contains F097

ldr 2

Absolute address is 099

Page 59: Chapter 4

ldr 2 loads what?

Page 60: Chapter 4

Index register

• Used to access arrays in main memory.

• H1 does not have a dedicated index register.

• The sp register can be used as an index register.

• But sp is normally not available—it is usually needed as the top-of-stack pointer.

Page 61: Chapter 4

The program on next slide sums the numbers in the array table using sp as an index register.

Page 62: Chapter 4
Page 63: Chapter 4

ldc tableswapst stk_svld countja test

loop: ld sumaddr 0st sumdloc 1 ; sp now points to &table[i]ld countsub @1st count

test: jnz loopldc msgsoutld sumdoutldc '\n'aouthalt

msg: dw “sum = “@1: dw 1count: dw 10sum: dw 0table: dw 56

dw -8dw 444

...

Problem: sp doesn't remainpointing at beginning of array

Page 64: Chapter 4

ldc tableswapst stk_svld countja test

loop: ld sumaddr countst sumld countsub @1st count

test: jzop loopldc msgsoutld sumdoutldc '\n'aouthalt

msg: dw “sum = “@1: dw 1count: dw 9sum: dw 0table: dw 56

dw -8dw 444

...

Solution: sp points to startof array and we calculatearray[i]

Page 65: Chapter 4

In place of the sp register, we can use a variable in memory as an index. See the next slide.

Page 66: Chapter 4

In a high-level languagewe would try to make indexand count do both things

Page 67: Chapter 4
Page 68: Chapter 4
Page 69: Chapter 4

call and ret instructions

• The call instruction saves the return address by pushing it onto the top of the stack (it pushes the pc register).

• The return instruction pops the top of the stack (which contains the return address) into the pc register.

Page 70: Chapter 4

12

Page 71: Chapter 4
Page 72: Chapter 4

12

Page 73: Chapter 4
Page 74: Chapter 4

The program on the next slide has three modules: a main module that calls f1 and f2.

Page 75: Chapter 4
Page 76: Chapter 4

Can we replace call/ret with ja instructions?

Yes, but what about the next slide?

Page 77: Chapter 4
Page 78: Chapter 4
Page 79: Chapter 4

Terminating instructions

• Halts terminates program. Must restart from beginning to continue (by entering o, then g or t).

• Bkpt stops execution—can restart from the current execution point (by entering the t or g commands). Used for debugging.

• 16-bit opcode

Page 80: Chapter 4

Assembler does not automatically generate instructions.

return is implicit

Page 81: Chapter 4

Some debugging commands• b12 sets breakpoint at location 12 hex• k (or b-) kills breakpoint• w20 sets watchpoint at location 20 hex• kw (or w-) kills watchpoint• mr sets “plus reads” mode• ms sets “plus source” mode• mso sets “plus source only” mode• mr- cancels “plus reads”• ms- cancels source modes

Page 82: Chapter 4

Plus source mode

---- [T7] 0: ld /0 018/ msMachine-level display mode + source---- [T7] 0: ld /0 018/ g 0: loop: ld sum ; get current sum ld /0 018/ ac=0000/0000 1: add n ; add n to it add /2 010/ ac=0000/0014

. . .

see the comments

Page 83: Chapter 4

Source-only mode

---- [T7] 0: ld /0 018/ mso

Machine-level display mode source only

---- [T7] 0: ld /0 018/ g

0: loop: ld sum ; get current sum

1: add n ; add n to it

.

.

.

Page 84: Chapter 4

Plus reads mode---- [T7] 0: ld /0 018/ mr

Machine-level display mode + reads

---- [T7] 0: ld /0 018/ g

0: ld /0 018/ 0000<m[018] ac=0000/0000

1: add /2 010/ 0014<m[010] ac=0000/0014

.

.

.

contents of m[018]

Page 85: Chapter 4

Watchpoint: execution stops when contents of the specified location changes.

Breakpoint: execution stops when instruction at the specified location is about to be executed.

Page 86: Chapter 4

May use labels to specify a location when setting a watchpoint or breakpoint if sim is in the plus source or source only mode. Otherwise, you must use absolute addresses.

wn (set watchpoint at ‘n’)

bloop (set breakpoint a ‘loop’)

w10 (set watchpoint a location 010)

b0 (se breakpoint at location 000)

Page 87: Chapter 4

Watchpoint

---- [T7] 0: ld /0 018/ wn

Watchpoint set at ‘n’ (loc 10)

---- [T7] 0: ld /0 018/ g (execute until n changes)

. . .

. . .

Watchpoint at ‘n’ (loc 10) m[010] = 11

---- [T7] 5: jz /C 008/

Page 88: Chapter 4

Breakpoint

---- [T7] 0: ld /0 018/ bloop

Breakpoint set at ‘loop’ (loc 20)

---- [T7] 0: ld /0 018/ g (execute )

. . .

. . .

Breakpoint at ‘loop’ (loc 20)

---- [T7] 20: sub /0 00F/

Page 89: Chapter 4

Cancel watchpoint with w- or kw

Cancel breakpoint with b- or k

Page 90: Chapter 4

More debugging commands• d100 display starting at location 100 hex

• d$ display stack

• d@ display memory pointed to by ac

• d* display all

• u% unassemble instructions to be executed next.

• p triggers partial trace

Page 91: Chapter 4

The p command is useful for detecting the location of an infinite loop.

Page 92: Chapter 4
Page 93: Chapter 4

More debugging commands

Page 94: Chapter 4
Page 95: Chapter 4

With the debugger, you can see how instructions work. The next slide shows how the push instruction works.

Page 96: Chapter 4
Page 97: Chapter 4

Using the debugger, it is easy to find the errors in the program on the next slide. This program calls a module that adds two numbers and returns the sum in the ac register.

Page 98: Chapter 4
Page 99: Chapter 4
Page 100: Chapter 4

The entry point is wrong. Use the j debugger command to start from the correct instruction (you can fix to source code later).

Page 101: Chapter 4
Page 102: Chapter 4

The ldr instruction is using the wrong relative address. Use the a command to assemble a new ldr instruction. Use o# so that the modification is not overlaid.

Page 103: Chapter 4
Page 104: Chapter 4
Page 105: Chapter 4

Memory-mapped I/O

• Associates an I/O device with a specific set of absolute addresses.

• Load and store operations to these addresses trigger I/O operations.

• Put ‘&’ at the beginning of an assembly language program to activate memory-mapped I/O.

Page 106: Chapter 4
Page 107: Chapter 4
Page 108: Chapter 4
Page 109: Chapter 4

equ directive

Page 110: Chapter 4
Page 111: Chapter 4

Little endian or big endian

Depends on how multiple-byte words are stored in byte-addressable memory

Page 112: Chapter 4
Page 113: Chapter 4

Little endian not good for string comparisons that hold the strings in registers—collating order is wrong.

See the next slide (‘C’ on the next slide is more significant than ‘A’).

Page 114: Chapter 4
Page 115: Chapter 4

Assume int is 4 bytes; short, 2 bytes. Then little endian is better for

int x = 0x00001234;short y;y = (short)x;

because you do not have to compute the location of the less significant half of x.

See the next slide.

Page 116: Chapter 4
Page 117: Chapter 4