Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Manual assembly

Options
  • 22-09-2009 7:40pm
    #1
    Registered Users Posts: 1,190 ✭✭✭


    Are there any tuts out there for this? Finding this a bit of a tough find on google. Mostly just get information on assembly in the general term of assembling a project for example or assembly tutorials.

    I would assume that it's not at least exceptionally hard?


Comments

  • Registered Users Posts: 3,945 ✭✭✭Anima


    What are you trying to do?


  • Registered Users Posts: 1,190 ✭✭✭wolfric


    manually assemble my code into binary


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    to do that, you'd have to write it in assembly for the target processor.
    you'd also have to understand the structure of executable binary format for target operating system.

    x86->ELF->Linux or x86->PE->Windows

    nasm or fasm would allow you to do this on x86.for other architectures, you'd probably have to use gnu toolchain assembler.

    good luck ;)


  • Registered Users Posts: 1,190 ✭✭✭wolfric


    Those are just assemblers though aren't they? I want to do what those tools would do manually or BY HAND.


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    well, if you're insane enough to write it in binary, an assembler will work.. :D


  • Advertisement
  • Registered Users Posts: 1,922 ✭✭✭fergalr


    I'm pretty sure:
    http://www.intel.com/products/processor/manuals/index.htm
    is what you are after.
    Specifically the instruction set references.

    You used to be able to get hard copies shipped to you for free - have a few somewhere. Wouldn't recommend it as a particularly effective way to spend your programming time - maybe if you looked at it in combination with using assembly and an assembler?


  • Registered Users Posts: 1,190 ✭✭✭wolfric


    i don't get you... perhaps i'm not understanding that well the concept of assembly.

    ^
    |
    programming
    ^
    |
    assembly
    ^
    |
    machine code/binary

    Assembly is written then assembled by the assembler to binary... Is there not some guide on how to do this by hand? What all the code should be translated to ? If i'm doing this by hand why would i need an assembler? It's like using an online translator if you had to convert french to english for an exam (not a direct metaphor there in that binary and assembly are not just different languages but hopefully you understand what i mean)

    Edit :just read the above post (posted this before i read it). I'll have a look through those manuals don't quite see what i need in there i haven't done much assembly atm but everything i do is purely for my own interest and learning. Hell i'd probably just use python or php if i actually wanted something done.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Wolfric, I think you'll love the book "The Art of Assembly" even though it's high level assembly but it will teach you a lot about computer architecture. It also shows you how to translate instructions if I remember correctly. There's a similator too.

    By the way, its free ebook: http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/www.artofasm.com/index.html

    :)


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    I'll have a look through those manuals don't quite see what i need in there i haven't done much assembly atm but everything i do is purely for my own interest and learning.

    You don't want to do assembly if the following:
    What are you trying to do?
    manually assemble my code into binary
    is true - what you want to do is basically write machine code. Some good info on the wiki article there.

    wolfric wrote: »
    Edit :just read the above post (posted this before i read it). I'll have a look through those manuals don't quite see what i need in there i haven't done much assembly atm but everything i do is purely for my own interest and learning. Hell i'd probably just use python or php if i actually wanted something done.

    Go to the link I provided, open the document called
    "Intel® 64 and IA-32 Architectures Software Developer's Manual
    Volume 2A: Instruction Set Reference, A-M"

    Go to the section called
    "CHAPTER 2
    INSTRUCTION FORMAT"
    and knock yourself out.

    Its a lot of documentation, but if you want to write machine code (that runs on your modern processor), by hand, that's the sort of information you need.

    If you have not done assembly before, I strongly recommend, that if you want to go down this road you start off by doing some assembly programming (and then quickly get bored and go back to python...) and then progress from there to doing a few machine code examples later, if that's what you want to do.

    Edit: I second the 'Art of Assembly' as a thorough introduction - again its time consuming, but so is the whole thing.
    Just to say, fair play to you for wanting to do this stuff - be warned it's very hardcore though, and there's not a lot to the process of manually translating assembly into binary - its just a very timeconsuming task. We had to (for the motorola, afair) do it as part of CS classes, using documentation like I linked to - just to do the process a few times to see what the assembler does for you, and it was a worthwhile exercise, but not something I think its worthwhile to do on a regular basis.


  • Registered Users Posts: 1,190 ✭✭✭wolfric


    Extremely efficient though when it comes to speed/memory/size. Too bad it would just eat mountains of your time for large programs. Thanks for that i always assumed that "machine code" was like just a general term mesh of 2 terms not a defined one.


  • Advertisement
  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    if you want to optimize code for speed, intel compilers imo are the best.

    Although some highly experienced assembly programmers might do better..the work involved is painful.

    Even those experienced who believe their code is better than compiler can be wrong.

    if you want to optimize for size and don't care about speed, if you need something really low-level or unorthodox, then you use assembly, since it gives you most control over the generation of code.. you've total freedom in how it's written, you can "break the rules" -- not have special privileges, but you could use registers of the processor the compiler would normally reserve for specific purposes or not touch at all.

    but it wouldn't make much sense to write large scale applications using assembly.

    on top of hard to find bugs, there's the obvious problem of maintainability -- assembly isn't suitable for that reason alone.

    you could write an object oriented program using assembly, but why bother when you have something like C++ or Java as examples?


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Extremely efficient though when it comes to speed/memory/size

    Just what type of program are you talking about? You probably won't do a better job than a compiler if the program is large enough.

    The only place I see assembly used is in time critical loops for DSP algorithms or things like that. No way would you write an entire large program in it. Its just not worth the time.


  • Registered Users Posts: 368 ✭✭backboiler


    wolfric wrote: »
    ^
    |
    programming
    ^
    |
    assembly
    ^
    |
    machine code/binary

    Assembly is written then assembled by the assembler to binary... Is there not some guide on how to do this by hand? What all the code should be translated to ?

    What you're looking for is the hardware manual for whatever processor you're interested in. It, or some document referenced in it, should contain the instruction set and machine code equivalent, possibly as a bit mapping pattern. RISC processor cores like ARM have a fairly regular mapping from bit patterns to assembly mnemonics. The same may not hold true for others like Intel processors with CISC ancestry.

    As for efficiency, there's nothing inherently efficient about assembly unless the programmer knows what they're doing. Sometimes hardware-level access requires specific access timing or patterns that only assembly can guarantee. Maybe a suitable execution environment may not yet exist (no stack/exception handlers) for higher level languages.


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    backboiler wrote: »
    What you're looking for is the hardware manual for whatever processor you're interested in. It, or some document referenced in it, should contain the instruction set and machine code equivalent, possibly as a bit mapping pattern.

    I think the documents I linked to above are the relevant ones? (This is assuming the OP is talking about x86 or 64). Or are you talking about some other sort of documentation?
    backboiler wrote: »
    RISC processor cores like ARM have a fairly regular mapping from bit patterns to assembly mnemonics. The same may not hold true for others like Intel processors with CISC ancestry.

    As for efficiency, there's nothing inherently efficient about assembly unless the programmer knows what they're doing.
    Sort of nitpicking here, but I don't fully agree with this statement.

    First off, its a confusing statement logically, because it seems to imply that if (and only if) a programmer knows what they are doing, the language is inherently efficient.
    Which is a strange definition of 'inherently' - surely an inherent property of a language would be fairly independent of the programmer?


    Maybe you are trying to say that assembly isn't inherently efficient?

    Now, I accept you can write slow code anywhere, and I'm not a big fan of coding in assembly.
    But in so far as a language can be inherently anything, I think its fair to say assembly is inherently small, and fast. It mightn't always be the smallest or fastest, and it could be tough to out-optimise your C compiler's global optimisations on a big code base, but assembly is probably going to generally do pretty well, against, say, Javascript, or Ruby - or a lot of languages really.

    I'm pretty sure its this ability to write fast and small code that the OP is getting at, rather than development time efficiency etc.
    As such, subject to certain caveats, I think the OPs comments were reasonable enough.

    I think its as silly to say that there's nothing inherently efficient about assembly as it is to say that everything should be coded in assembly for efficiency.


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    think what backboiler means, it's better to leave the assembly code generation to a compiler unless the programmer really knows what he's doing.

    i have seen some inline assembly at times where you could immediately say it would run slower than compiler code.

    on x86 atleast, they'd have used something like..

    [PHP] mov ecx,0
    lea ebx,[array]
    @fill_array:
    imul eax,ecx,4
    mov dword ptr[ebx+eax],ecx
    add ecx,1
    cmp ecx,256
    jne @fill_array[/PHP]

    whereas it would be better written like

    [PHP] mov ecx,255
    @fill_array:
    mov dword ptr[array+4*ecx], ecx
    dec ecx
    jns @fill_array[/PHP]

    or better with unrolled and paired execution.

    if an inexperienced assembly programmer wanted an if statement like

    [PHP]if (a > b) a = b;[/PHP]

    coder and even compiler might use

    [PHP] cmp eax,ebx
    jbe @continue
    mov eax,ebx
    @continue:[/PHP]

    good asm programmer would use something like

    [PHP] sub ebx, eax
    sbb ecx, ecx
    and ecx, ebx
    add eax, ecx[/PHP]

    or more lately..

    [PHP] cmp eax,ebx
    cmova eax,ebx[/PHP]

    since neither use a branch, both should run faster.

    [PHP]if (a != 0)
    a = b;
    else
    a = c;[/PHP]

    equivilant.

    [PHP]cmp eax, 1
    sbb eax, eax
    and ecx, eax
    xor eax, -1
    and eax, ebx
    or eax, ecx[/PHP]

    again no branch, lots of ways to speed up code in asm but intel compilers kick ass.


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    wolfric wrote: »
    manually assemble my code into binary
    Mel? :pac:


Advertisement