Monday, 1 January 2018

Assembly Language - Part 1

Intro - BBC Micro Assembly

When I was a child (a long time ago) I used to program a BBC Micro (or "Beeb") circa 1982. A Beeb had two programming languages, BBC Basic and 6502 Assembly language because it had a 6502 CPU. Nearly all the programs from those days were typed in from a magazine like BBC Micro User, most of them were BBC Basic but some had an assembly section delimited by square brackets to speed up, here is an example taken from this screenshot


>10 oswrch=&FFEE
>20 DIM memory% 100
>30 FOR Z=0 TO 3 STEP 3
>40 [OPTZ
>50 .start LDA#ASC"!"
>60 LDX #40
>70 .loop jsr oswrch
>80 dex:BNE loop
>90 rts:] NEXT Z
>100 CALL start
>110 END

I couldn't tell you what the above program does, it was a long time ago. But I can tell you that a high level programmer takes for granted writing an expression such as a=b+c. Under the hood the expression is translated into assembly operations: an accumulator is loaded with a memory call to the address of b and the same accumulator accumulates (hence its name) the sum with a memory call to the address of c and then the result is stored in the memory address of a.

So it's about marshalling data from memory into a register, doing an op and then storing the result. You may think 'so what'? But sometimes, a discussion of C/C++ compilers descends into assembly language and it is nice to have an inkling of how it works. But better to turn now to Intel Assembly Language.

Intel - x86 and x64 Assembly

So I won't reproduce the contents of the following links but will keep some notes. First the links.

Registers

Section16 bit CodeDescLong Desc32-bit64-bit
General Purpose RegistersAX AccumulatorMain arithmetic registerEAXRAX
BXBaseGenerally used as a memory base or offsetEBXRBX
CXCounterGenerally used as a counter for loopsECXRCX
DXDataGeneral 16-bit storage, division remainderEDXRDX
Offset RegistersIPInstruction pointerCurrent instruction offset
SPStack pointerCurrent stack offsetESPRSP
BPBase pointerBase for referencing values stored on stackEBPRBP
SISource indexGeneral addressing, source offset in string opsESIRSI
DIDestination indexGeneral addressing, destination in string opsEDIRDI
Segment RegistersCSCode segmentSegment to which IP refers
SSStack segmentSegment to which SP refers
DSData segmentGeneral addressing, usually for program's data area
ESExtra segmentGeneral addressing, destination segment in string ops

  Flags Register (Respectively bits 11,10,9,8,7,6,4,2,0)
    OF  Overflow flag  : Indicates a signed arithmetic overflow occurred
    DF  Direction flag : Controls incr. direction in string ops (0=inc, 1=dec)
    IF  Interrupt flag : Controls whether interrupts are enabled
    TF  Trap flag      : Controls debug interrupt generation after instructions
    SF  Sign flag      : Indicates a negative result or comparison
    ZF  Zero flag      : Indicates a zero result or an equal comparison
    AF  Auxiliary flag : Indicates adjustment is needed after BCD arithmetic
    PF  Parity flag    : Indicates an even number of 1 bits
    CF  Carry flag     : Indicates an arithmetic carry occurred


Why do registers matter? It would appear that it is possible to get a compiler to pass variables not via the stack but via registers for extra speed. See Intel's C/C++ Calling Conventions.

No comments:

Post a Comment