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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Learning assembly - HLA

  • 09-10-2009 10:47am
    #1
    Closed Accounts Posts: 248 ✭✭


    Ive started learning assembly with the HLA Art of Assembly guide -http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/www.artofasm.com/Windows/HTML/AoATOC.html

    What is the consensus on this guide, is it a good way to learn assembly? Is it outdated in that the code examples won't run properly on modern hardware? Some of the code examples I tried compiled ok but the programs themselves did not run properly. I then tried copying and pasting the code directly from the guide and compiling again but it still didn't work. So is the guide no longer good for modern hardware?


Comments

  • Closed Accounts Posts: 248 ✭✭bSlick


    Here's a picture of a simple problem with the output it produces. If anyone is familiar with HLA I'd appreciate if you could tell me what's going wrong here.

    HLA.png


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


    never really saw any point to using HLA even though it was intended for beginning assembly programmers.

    i started with TASM, then MASM32 and finally JWASM with Win32 Include files by same author - MASM32 package sucks and TASM was never updated.
    There's also FASM, NASM, YASM...if you want to take a look at those.

    You'd be better off using only assembly instructions and avoid using high-level constructs such as IF/WHILE/FOR unless you plan on writing big applications in assembly.

    Learn C or some other HLL if you want to use high-level ideas...

    If you have Visual C++ or mingw installed, you can generate assembly output for code in C, this is also a good way to learn.

    example..

    [PHP]#include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char* argv[])
    {
    if(argc != 3) {
    printf("\nUsage:%s <num1> <num2>\n",argv[0]);
    return 0;
    }

    int a = atoi(argv[1]);
    int b = atoi(argv[2]);

    printf("\n%i + %i = %i",a,b,(a+b));
    return 0;
    }[/PHP]

    Run from command line: gcc -O2 -S code.c -masm=intel
    And you'll get output like..

    [PHP] cmp DWORD PTR [ebp+8], 3 ; argc == 3?
    je L2 ; jump if equal

    mov eax, DWORD PTR [esi] ; argv[0]
    mov DWORD PTR [esp], OFFSET FLAT:LC0 ; "\nUsage:%s <num1> <num2>\n"
    mov DWORD PTR [esp+4], eax ;
    call _printf

    mov ebx, DWORD PTR [ebp-8]
    xor eax, eax
    mov esi, DWORD PTR [ebp-4]
    mov esp, ebp
    pop ebp
    ret
    .p2align 4,,7
    L2:
    mov eax, DWORD PTR [esi+4] ; argv[1]
    mov DWORD PTR [esp], eax
    call _atoi

    mov ebx, eax ; ebx = a
    mov eax, DWORD PTR [esi+8] ; argv[2]
    mov DWORD PTR [esp], eax
    call _atoi

    mov DWORD PTR [esp+8], eax ; 3rd parameter is value of b
    lea edx, [ebx+eax] ; edx = a + b
    mov DWORD PTR [esp+4], ebx ; 2nd parameter is value of a
    mov DWORD PTR [esp+12], edx ; 4th is a + b
    mov DWORD PTR [esp], OFFSET FLAT:LC1 ; "\n%i + %i = %i"
    call _printf[/PHP]

    I added comments here, but Microsoft Visual studio will do something similar (i think) also including some source code using right switch..

    [PHP]int main(int argc, char* argv[])
    {
    int i,count;

    if(argc != 2) {
    printf("\nUsage:%s <loop count>\n",argv[0]);
    return 0;
    }

    count = atoi(argv[1]);

    for(i = 0;i < count;i++)
    printf("\nLine %d",i+1);

    return 0;
    }[/PHP]

    [PHP]L2:
    mov eax, DWORD PTR [ebx+4] ; argv[1]
    mov DWORD PTR [esp], eax
    call _atoi
    mov esi, eax ; esi = count
    xor eax, eax ; i = 0
    cmp eax, esi ; i >= count?
    jge L1 ; exit if greater or equal
    .p2align 4,,15
    L12:
    mov DWORD PTR [esp], OFFSET FLAT:LC1 ; 1st parameter "\nLine %d\0"
    lea ebx, [eax+1] ;
    mov DWORD PTR [esp+4], ebx ; 2nd parameter (i + 1)
    call _printf

    mov eax, ebx
    cmp eax, esi ; i < count ?
    jl L12 ; jump if less[/PHP]

    Note if you changed int i,count; to unsigned int i,count;

    You'll get different code generated:

    unsigned comparisons

    [PHP] cmp eax, esi ; i >= count?
    jae L1 ; exit if greater or equal[/PHP]

    [PHP] cmp eax, esi ; i < count ?
    jb L12 ; jump if less[/PHP]

    just as with moving these types, you'll see MOVSX used for signed types, MOVZX for unsigned...you won't learn or spot these little details using HLA.


  • Closed Accounts Posts: 248 ✭✭bSlick


    Martyr wrote: »
    never really saw any point to using HLA even though it was intended for beginning assembly programmers.

    i started with TASM, then MASM32 and finally JWASM with Win32 Include files by same author - MASM32 package sucks and TASM was never updated.
    There's also FASM, NASM, YASM...if you want to take a look at those.

    You'd be better off using only assembly instructions and avoid using high-level constructs such as IF/WHILE/FOR unless you plan on writing big applications in assembly.

    Learn C or some other HLL if you want to use high-level ideas...

    I already know C and PHP, I'm just looking to learn assembly so I can understand what's going on behind the scenes better. I don't plan on writing many, if any, applications in it. I was using hla because it seemed like the most comprehensive guide I could find on the net regarding assembly.

    I've no problem ditching hla and going with something else tho, especially if simplifies or abstracts regular assembly too much. I'm also not too impressed that some of the most basic examples in the guide don't even work properly.

    Are there any guides out there you could recommend to me to learn assembly? And what's the deal with the various assembly packages such as TASM, MASM, NASM, etc... is the code used for each significantly different and which one would you recommend I should use?


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


    Are there any guides out there you could recommend to me to learn assembly?

    You might want to try this this

    This is also very useful reference and FASM has an excellent PDF.

    intel processor manuals

    For windows specific programming, lots of people get directed to Iczelion's page, it's old now.. but still worth a look.

    For linux programming, try FASM forum, but also the linux assembly page has great examples.

    i always found reading other peoples source code useful, reversing programs, debugging and spending lots of time writing my own.

    join a couple of message boards, flat assembler forum is good start.
    And what's the deal with the various assembly packages such as TASM, MASM, NASM, etc... is the code used for each significantly different and which one would you recommend I should use?

    Check out this page here for some info.

    The only real differences are control and preprocessor directives, how you address variables, write macros..that's about it.

    Once you know assembly, using other assemblers is no problem.

    Although i started with TASM, that's only because it's all i had available at the time. i'd avoid it if i were you.

    Lots of people liked it because it allowed sloppy mistakes, but the version freely available doesn't support instructions older than a pentium/586,although people have written macros..don't bother with them.

    next assembler i tried was MASM, more specifically the MASM32 package, MASM32 is popular but poorly maintained and has stupid limitations by the packages author - avoid it like the plague..

    i eventually switched to using MASM on its own along with japheths win32 include headers, this is what i'd still use today if not for jwasm..

    NASM is good if you want to write code for both windows/linux, there are little minor things i don't like about it.
    Mainly how it encodes displacement values and relative jumps, but it's well written and latest version has good support for latest opcodes.

    NASMX is something like MASM32, i've never tried it but might be worth a look...

    YASM was a complete re-write of NASM 0.98, never used it.

    JWASM has support for MASM syntax, support for ELF/PE, runs on both linux/windows, and the author has created his own set of Win32 Include files which are extremely helpful.

    if i had a top 3, it'd be
    1. JWASM with win32 include headers
    2. NASM
    3. FASM


  • Closed Accounts Posts: 248 ✭✭bSlick


    Ok thanks for all the help mate.


  • Advertisement
Advertisement