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

Urgent: Need a quick primer on Assembly.

Options
  • 12-01-2008 2:17am
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Hi,

    I have an exam on monday and part of it will be on assembly.

    I've just gone through some of the notes there and they are very sketchy on how to write the program exactly..

    I've been told to use NASM to compile a program using the command(which I can't understand)
    nasm -f bin -o myprogram.com myprogram.asm
    

    I've also been told that an asm program contains three parts:
    SECTION.text
    SECTION.data
    SECTION.bss

    is this all in one file (with what extension)?
    or are these 3 seperate files?

    Here is the code i've been told to write and compile:
    BITS 16
    	  	ORG 0x100
    SECTION .text
    start
    	  mov dx,hellostring	;Move hellostring to dx.	
    	  mov ah,9h		;9h is the DOS printing program.
    	  int 0x21		;Call DOS.
    	  mov ax, 0x4c00		;Program termination.	
    	  int 0x21		;Call DOS
    
    SECTION .data
    hellostring	  db 'Hello world from assembly!', 13, 10, '$'
    
    SECTION .bss
    ;Nothing here for this program.
    

    My notes in general are really vague:mad::mad:
    Does anybody have any links to a quick primer in ASM?

    Sorry if I am being a bit rough here, but i've run out of steam at this stage..
    Thanks,
    Techguy


Comments

  • Registered Users Posts: 1,636 ✭✭✭henbane




  • Closed Accounts Posts: 10,833 ✭✭✭✭Armin_Tamzarian


    nasm -f bin -o myprogram.com myprogram.asm

    This tells the computer to use the operating system to compile the Assembly Language program stored in the file "myprogram.asm" into the binary COM file myprogram.com -f and -o are standard 'switches' that tell nasm that you are specifying the outputfile name and the output file format.


    I've also been told that an asm program contains three parts:
    SECTION.text
    SECTION.data
    SECTION.bss

    is this all in one file (with what extension)?
    or are these 3 seperate files?

    One file for all three sections.

    start
    //hellostring is copied to the dx register.
    mov dx,hellostring ;Move hellostring to dx.
    mov ah,9h ;9h is the DOS printing program.
    //An Interrupt is called which causes the contents of the dx register
    //to be printed out on screen.
    int 0x21 ;Call DOS.
    mov ax, 0x4c00 ;Program termination.
    int 0x21 ;Call DOS

    SECTION .data
    //You're declaring a variable called 'hellostring'.
    //You're assigning the value of "Hello world from assembly!' to hellostring.
    hellostring db 'Hello world from assembly!', 13, 10, '$'

    PM me if you get stuck. I taught assembly in the past but it was a long time ago.


  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    Attempt Two to post this message, f**king boards.ie logged me out and since I was in quick reply my previous reply was lost...grrrr :mad: Remember I coded ASM for windows 95 and used MASM (not NASM) but most of the same things apply :D
    techguy wrote: »
    I've been told to use NASM to compile a program using the command(which I can't understand)
    nasm -f bin -o myprogram.com myprogram.asm
    

    This is the command line to compile the program using NASM.

    I have no idea what -f bin does. I guess it means output file format is to be a binary COM instead of a .exe If you want you can try use "nasm -h", "nasm --help" or "nasm /?" to find out what it means.

    -o myprogram.com => compiled program will be called myprogram.com. You can change it to whatever like techguy.exe or crap_program.com (maybe stick with .com since it's what you're using above). As a convention you usually name it the same as the assembly file and only change the extension (e.g. test.asm => test.com)

    myprogram.asm => the plaintext (paintext) file with the assembly code in it.
    I've also been told that an asm program contains three parts:
    SECTION.text
    SECTION.data
    SECTION.bss

    is this all in one file (with what extension)?
    or are these 3 seperate files?

    Same file - different sections. They help the compiler and linker know which is which. As you can see from your code, .text is for the program code and .data is where you declare your variables (like the hellostring) and I never used a .bss when I was doing this stuff for a very brief module 2 years ago
    Here is the code i've been told to write and compile:
    BITS 16
    	  	ORG 0x100
    SECTION .text
    start
    	  mov dx,hellostring	;Move hellostring to dx.	
    	  mov ah,9h		;9h is the DOS printing program.
    	  int 0x21		;Call DOS.
    	  mov ax, 0x4c00		;Program termination.	
    	  int 0x21		;Call DOS
    
    SECTION .data
    hellostring	  db 'Hello world from assembly!', 13, 10, '$'
    
    SECTION .bss
    ;Nothing here for this program.
    

    Firstly we'll deal with the .DATA section

    Here you see a variable called hellostring, which is a double-word type (db). Its value is 'Hello world from assembly!'. However, to get a new line to appear after the sentence, a 13,10 is added on the end (13 = carriage return, 10 = new line). If you don't understand then you can look up the ASCII table for numbers 10 and it will be an LF (Line feed) and 13 = CR. Suffice to say that *nix and Windows differ on what's necessary for a new line to show up. The '$' is just a token that means EOS (End of String).

    Next up, on a processor you have ultra-fast "memory" spaces called registers. These can be 16 bit (AX, BX ...) or 32-bin (EAX, EBX ...E=extended). Since your program has a BITS 16, you will only use AX, BX, CX, etc. Some registers are used for processes and shouldnt really be used to store data. For example, if you were to run a loop, CX is used as a counter (c[x]ounter) so it wouldn't be wise to have stuff in there when ya start your loop. Anyways....

    Each operating system has a series of interrupts and subroutines it keeps track of. To call an interrupt is to "pause" the program at that point, run the subroutine and then come back and continue. It is a control flow thing. Int 21h (21h = 0x21) is a powerful interrupt :D

    By moving 9h into the DX register and calling int 21h, the OS will pause your program and do stuff. You are told 9h is the subroutine to print out stuff. You should be able to guess that since you moved the memory location of hellostring (NOT the string itself, it's memory location) into the AX register, that the subroutine takes the address in the AX register and starts printing it out until it hits a $ sign or dies due to some error (BSOD :P).

    After thats done, another value is moved into the AX register (4ch or 0x4c00) and int 21h is called again. 4ch is the subroutine to cease program execution so basically it means to exit to dos and quit your program :)

    Experiment, chuck the code into a text file, call it myprogram.asm and then compile it using NASM - change around the text a bit.. maybe instead of '$' put in ' - it works, I rock!$' or something. Try without the 10, 13 and see what I meant about the newline thing.

    And welcome to assembly, soon they will have you programming LED's out of the printer port and maybe programming your own interrupt routine that prints out how assembly rules on the screen - you may even be introduced to TSR's (Terminate and stay resident routines...) but I'll stop frightening you now :P
    My notes in general are really vague:mad::mad:
    Does anybody have any links to a quick primer in ASM?

    Sorry if I am being a bit rough here, but i've run out of steam at this stage..
    Thanks,
    Techguy

    Your notes not have a recommended text? :p Check out "Art Of Assembly" on Google - nice few resources there


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    Here is a list of dos interrupt codes:

    http://spike.scu.edu.au/~barry/interrupts.html

    Take for example the first one:
    AH = 01h - READ CHARACTER FROM STANDARD INPUT, WITH ECHO Return: AL = character read

    This means to read a character from the keyboard (which is standard input), we move the value 01h (h is for hexadecimal) into the register AH (a register is just a thing that can store a value.

    We do this with the instruction
    mov AH, 01h

    which is most easily read as "move into AH the value 01h". Assembly becomes much easier if you can translate all of these instructions into this kind of english.

    Then we call the DOS interrupt 21h using
    int 21h or int 0x21 depending on the compiler (in your case I think NASM uses 0x21).

    When the function finishes, the register AL will have the value of the key pressed.

    More stuff:
    ORG 0x100 means "put all this stuff at memory address 0x100" (relative to the start of the program).

    hellostring db 'Hello world from assembly!', 13, 10, '$'
    All of the characters have a numeric value (see http://asciitable.com/), the values "13" and "10" together are equivalent to pressing enter. I think the '$' signifies the end of the string.


    I don't usually do assembly with DOS but if you're stuck feel free to PM me.


  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    hellostring db 'Hello world from assembly!', 13, 10, '$'
    All of the characters have a numeric value (see http://asciitable.com/), the values "13" and "10" together are equivalent to pressing enter. I think the '$' signifies the end of the string.

    Spot on :)


  • Advertisement
  • Registered Users Posts: 2,234 ✭✭✭techguy


    Thaks for all your help guys, i'll have look through it all.


Advertisement