Download presentation
Presentation is loading. Please wait.
Published byLoreen Houston Modified over 8 years ago
1
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville
2
Assembly files (.asm) We will be using the nasm assembler Program Components Comments Labels Directives Data Main subroutine, which is a global one Instructions: generally the format of an NASM instruction is as follows Label Instruction Operands ; Comment
3
Program Organization : Skeleton file ; file: skel.asm ; This file is a skeleton that can be used to start assembly programs. %include "asm_io.inc" segment.data ; initialized data is put in the data segment here segment.bss ; uninitialized data is put in the bss segment segment.text global asm_main asm_main: enter 0,0 ; setup routine pusha ; code is put in the text segment. Do not modify the code before ; or after this comment. popa mov eax, 0 ; return back to C leave ret
4
Assembly I/O Routines %include “asm_io.inc” print_int prints out to the screen the value of the integer stored in EAX print_char prints out to the screen the character whose ASCII value stored in AL print_string prints out to the screen the contents of the string at the address stored in EAX. The string must be a Ctype string (i.e. null terminated) print_nl prints out to the screen a new line character read_int reads an integer from the keyboard and stores it into the EAX register read_char reads a single character from the keyboard and stores its ASCII code into the EAX register Uses a CALL instruction to invoke above routines
5
Comments Comments are denoted by semi-colons (;). Everything from the semi-colon to the end of the line is ignored.
6
Labels Labels identify The start of subroutines or locations to jump to in your code Variables are declared as labels pointing to specific memory locations Labels are local to your file/module unless you direct otherwise The colon identifies a label (an address!) Example: NewLabel: To define a label as global we say globalNewLabel
7
Directives Direct the assembler to do something Define constants Define memory to store data into Group memory into segments Conditionally include source code Include other files
8
Equ and % define directives The equ directive Used to define named constants used in your assembly program Syntax: symbol equ value Similar to C’s const directive :(const int symbol = value) The %define directive Similar to C’s #define directive ( #define name value) Most commonly used to define constant macros: %define SIZE 100 moveax, SIZE Macros can be redefined, and can be more complex than simple constants
9
Data directives Used in data segments to define room for memory There are two ways memory can be reserved Defines room for data without initial value ( segment.bss) Using : RESX directive Defines room for data with initial value (segment.data) Using : DX directive Note: X is replaced with a letter that determines the size of the object as following
10
Example: Data Directives L1db0;byte labeled L1 w/ initial value 0 decimal L2dw1000;word labeled L2 w/ initial value 1000 decimal L3db110101b;byte labeled L3 w/ initial value 110101 binary( 53) L4db12h;byte labeled L4 w/ initial value 12 hex (18 decimal) L5 db17o;byte labeled L5 w/ initial value 17 octal (15 decimal) L6dd1A92h;doubleword labeled L6 initialized to hex 1A92 L7resb1;1 uninitialized byte L8db“A”;byte initialized to ASCII of A = 65 L9 resw 100 ; reserves room for 100 words Note: Double quotes and single quotes are treated the same
11
More examples Sequences of memory may also be defined. L10 db 0, 1, 2, 3 ; defines 4 bytes L11 db "w", "o", "r", ’d’, 0 ; defines a C string = "word" L12 db ’word’, 0 ; same as L11 For large sequences, NASM’s TIMES directive is often useful. L13 times 100 db 0 ; equivalent to 100 (db 0)’s
12
Assembling the code Use PuTTy and WinSCP application to use nasm commands To edit your assembly file nano myfile.asm (or vi or emacs or joe or WinSCP editor ) To assemble your program nasm –f elf myfile.asm To create an executable program gcc –m32 myfile.o driver.c asm_io.o From the above command, you will get a new prompt and a file a.out or a.exe will be created To run the program, give the command./a.out or./a.exe
13
Program Organization for CS3230 Generally, we will be using a C driver program called driver.c to run our assembler routines Why driver.c ? 1. lets the C system set up the program to run correctly 2. All the segments and their corresponding segment registers will be initialized by C 3. The C library will also be available to be used by the assembly code
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.