Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Low-level programming language
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Assembly language == {{Main|Assembly language}} Second-generation languages provide one abstraction level on top of the machine code. In the early days of coding on computers like [[TX-0]] and [[PDP-1]], the first thing [[MIT]] [[Hacker culture|hackers]] did were write assemblers.<ref name=":1">{{cite book|last=Levy|first=Stephen|year=1994|title=Hackers: Heroes of the Computer Revolution|title-link=Hackers: Heroes of the Computer Revolution|publisher=Penguin Books|page=32|isbn=0-14-100051-1}}</ref> Assembly language has little [[Semantics (computer science)|semantics]] or formal specification, being only a mapping of human-readable symbols, including symbolic addresses, to [[opcode]]s, [[memory address|addresses]], numeric constants, [[string (computer science)|strings]] and so on. Typically, one [[machine instruction (computing)|machine instruction]] is represented as one line of assembly code, commonly called a ''mnemonic''.<ref>{{Cite web |title=Machine Language/Assembly Language/High Level Language |url=https://www.cs.mtsu.edu/~xyang/2170/computerLanguages.html |access-date=2024-04-27 |website=www.cs.mtsu.edu |archive-url=https://web.archive.org/web/20241214053921/https://www.cs.mtsu.edu/~xyang/2170/computerLanguages.html |archive-date=2024-12-14 |url-status=dead}}</ref> Assemblers produce [[object file]]s that can [[linker (computing)|link]] with other object files or be [[loader (computing)|loaded]] on their own. Most assemblers provide [[macro (computer science)|macros]] to generate common sequences of instructions. Example: The same [[Fibonacci number]] calculator as above, but in [[x86 assembly language|x86-64 assembly language]] using [[Intel syntax]]: <syntaxhighlight lang="asm"> fib: mov rax, rdi ; The argument is stored in rdi, put it into rax test rdi, rdi ; Is the argument zero? je .return_from_fib ; Yes - return 0, which is already in rax cmp rdi, 2 ; No - compare the argument to 2 jbe .return_1_from_fib ; If it is less than or equal to 2, return 1 mov rcx, rdi ; Otherwise, put it in rcx, for use as a counter mov rdx, 1 ; The first previous number starts out as 1, put it in rdx mov rsi, 1 ; The second previous number also starts out as 1, put it in rsi .fib_loop: lea rax, [rsi + rdx] ; Put the sum of the previous two numbers into rax cmp rcx, 2 ; Is the counter 2? je .return_from_fib ; Yes - rax contains the result mov rsi, rdx ; No - make the first previous number the second previous number dec rcx ; Decrement the counter mov rdx, rax ; Make the current number the first previous number jmp .fib_loop ; Keep going .return_1_from_fib: mov rax, 1 ; Set the return value to 1 .return_from_fib: ret ; Return </syntaxhighlight> In this code example, the [[Processor register|registers]] of the x86-64 processor are named and manipulated directly. The function loads its 64-bit argument from {{code|rdi}} in accordance to the [[x86 calling conventions#System V AMD64 ABI|System V application binary interface for x86-64]] and performs its calculation by manipulating values in the {{code|rax}}, {{code|rcx}}, {{code|rsi}}, and {{code|rdi}} registers until it has finished and returns. Note that in this assembly language, there is no concept of returning a value. The result having been stored in the {{code|rax}} register, again in accordance with System V application binary interface, the {{code|ret}} instruction simply removes the top 64-bit element on the [[Stack-based memory allocation|stack]] and causes the next instruction to be fetched from that location (that instruction is usually the instruction immediately after the one that called this function), with the result of the function being stored in {{code|rax}}. x86-64 assembly language imposes no standard for passing values to a function or returning values from a function (and in fact, has no concept of a function); those are defined by an [[application binary interface]] (ABI), such as the System V ABI for a particular instruction set. Compare this with the same function in [[C (programming language)|C]]: <syntaxhighlight lang="c"> unsigned int fib(unsigned int n) { if (!n) { return 0; } else if (n <= 2) { return 1; } else { unsigned int f_nminus2, f_nminus1, f_n; for (f_nminus2 = f_nminus1 = 1, f_n = 0; ; --n) { f_n = f_nminus2 + f_nminus1; if (n <= 2) { return f_n; } f_nminus2 = f_nminus1; } } } </syntaxhighlight> This code is similar in structure to the assembly language example but there are significant differences in terms of abstraction: * The input (parameter {{code|n}}) is an abstraction that does not specify any storage location on the hardware. In practice, the C compiler follows one of many possible [[calling convention]]s to determine a storage location for the input. * The local variables {{code|f_nminus2}}, {{code|f_nminus1}}, and {{code|f_n}} are abstractions that do not specify any specific storage location on the hardware. The C compiler decides how to actually store them for the target architecture. * The return function specifies the value to return, but does not dictate ''how'' it is returned. The C compiler for any specific architecture implements a '''standard''' mechanism for returning the value. Compilers for the x86-64 architecture typically (but not always) use the {{code|rax}} register to return a value, as in the assembly language example (the author of the assembly language example has ''chosen'' to use the System V application binary interface for x86-64 convention but assembly language does not require this). These abstractions make the C code compilable without modification on any architecture for which a C compiler has been written, whereas the assembly language code above will only run on processors using the x86-64 architecture.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)