Asm/fi: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Asm/fi
| Old | New | Line | |
|---|---|---|---|
| 1 | + | [[Reserved word|Varattu sana]] `asm` aloittaa [[Block|lohkon]] sisäiselle [[Assembly language/fi|assembly]] koodille. | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | program asmDemo(input, output, stderr); | |
| 5 | + | ||
| 6 | + | // The $asmMode directive informs the compiler | |
| 7 | + | // which syntax is used in asm-blocks. | |
| 8 | + | // Alternatives are 'att' (AT&T syntax) and 'direct'. | |
| 9 | + | {$asmMode intel} | |
| 10 | + | ||
| 11 | + | var | |
| 12 | + | n, m: longint; | |
| 13 | + | begin | |
| 14 | + | n := 42; | |
| 15 | + | m := -7; | |
| 16 | + | writeLn('n = ', n, '; m = ', m); | |
| 17 | + | ||
| 18 | + | // instead of declaring another temporary variable | |
| 19 | + | // and writing "tmp := n; n := m; m := tmp;": | |
| 20 | + | asm | |
| 21 | + | mov eax, n // eax := n | |
| 22 | + | // xchg can only operate at most on one memory address | |
| 23 | + | xchg eax, m // swaps values in eax and at m | |
| 24 | + | mov n, eax // n := eax (holding the former m value) | |
| 25 | + | // an array of strings after the asm-block closing 'end' | |
| 26 | + | // tells the compiler which registers have changed | |
| 27 | + | // (you don't wanna mess with the compiler's notion | |
| 28 | + | // which registers mean what) | |
| 29 | + | end ['eax']; | |
| 30 | + | ||
| 31 | + | writeLn('n = ', n, '; m = ', m); | |
| 32 | + | end. | |
| 33 | + | ``` | |
| 34 | + | ||
| 35 | + | Jotta saataisiin siirrettävyys eri alustojen välillä (eli koodi voitaisiin edelleen koota moniin ympäristöihin), samalla kun halutaan optimoida tiettyjä laitteita, niin asetetaan [[Conditional compilation|ehdollinen kääntäminen]]: | |
| 36 | + | ||
| 37 | + | ``` pascal | |
| 38 | + | program sign(input, output, stderr); | |
| 39 | + | ||
| 40 | + | type | |
| 41 | + | signumCodomain = -1..1; | |
| 42 | + | ||
| 43 | + | { returns the sign of an integer } | |
| 44 | + | function signum(const x: longint): signumCodomain; | |
| 45 | + | {$ifdef CPUx86_64} // ============= optimized implementation | |
| 46 | + | assembler; | |
| 47 | + | {$asmMode intel} | |
| 48 | + | asm | |
| 49 | + | // load constants: cmov cannot handle immediates | |
| 50 | + | // xor-instruction modifies flags => put it prior test | |
| 51 | + | xor r8, r8 // r8 := 0 | |
| 52 | + | ||
| 53 | + | // comparison pulled up front for pipelining | |
| 54 | + | test x, x // x = 0 ? | |
| 55 | + | ||
| 56 | + | // load constants, since cmov cannot handle immediates | |
| 57 | + | mov r9, -1 // r9 := -1 | |
| 58 | + | ||
| 59 | + | // determine result | |
| 60 | + | mov eax, 1 // result := 1 | |
| 61 | + | cmovl eax, r9 // if x < 0 then result := -1 | |
| 62 | + | cmove eax, r8 // if x = 0 then result := 0 | |
| 63 | + | end; | |
| 64 | + | {$else} // ========================== default implementation | |
| 65 | + | begin | |
| 66 | + | // This is what virtually math.sign does. | |
| 67 | + | // The compiled code requires _two_ cmp instructions, though. | |
| 68 | + | if x > 0 then | |
| 69 | + | begin | |
| 70 | + | signum := 1; | |
| 71 | + | end | |
| 72 | + | else if x < 0 then | |
| 73 | + | begin | |
| 74 | + | signum := -1; | |
| 75 | + | end | |
| 76 | + | else | |
| 77 | + | begin | |
| 78 | + | signum := 0; | |
| 79 | + | end; | |
| 80 | + | end; | |
| 81 | + | {$endif} | |
| 82 | + | ||
| 83 | + | // M A I N ================================================= | |
| 84 | + | var | |
| 85 | + | x: longint; | |
| 86 | + | begin | |
| 87 | + | readLn(x); | |
| 88 | + | writeLn(signum(x)); | |
| 89 | + | end. | |
| 90 | + | ``` | |
| 91 | + | ||
| 92 | + | Kuten nähdään niin voidaan toteuttaa kokonaisia rutiineja assembly kielellä lisäämällä `assembler` modifioija ja kirjoittamalla `asm` sen sijaan, että se aloitettaisiin [[Begin/fi|begin]] toteutuslohkolla. | |
| 93 | + | ||
| 94 | + | ## Katso myös | |
| 95 | + | ||
| 96 | + | Yleistä | |
| 97 | + | ||
| 98 | + | - [[The inline assembler parser]] | |
| 99 | + | - [[Lazarus inline assembler]] | |
| 100 | + | - [[Label|label § “assembler”]] | |
| 101 | + | ||
| 102 | + | Merkitykselliset kääntäjänohjeet | |
| 103 | + | ||
| 104 | + | - [[sAsmmode|{$asmMode}]] | |
| 105 | + | - [[sGoto|{$goto}]] | |
| 106 | + | - [[sStackFrames|{$stackframes}]] | |
| 107 | + | ||
| 108 | + | Erityistehtävät | |
| 109 | + | ||
| 110 | + | - [[Hardware Access|hardware access]] | |
| 111 | + | - [[AVR Programming|AVR programming]] | |
| 112 | + | ||
| 113 | + | --- | |
| 114 | + | *From the Free Pascal wiki, page "Asm/fi", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
