Manual R4
Manual R4
Aim
To write an X86/64 assembly language program that counts the number of
positive and negative numbers in a predefined array.
Apparatus Required
1. Computer System: A computer with a 64-bit processor (Intel i5 or
equivalent).
2. Assembler: An assembler like NASM (Netwide Assembler) for X86/64
architecture.
3. Debugger: A debugger such as GDB (GNU Debugger) for debugging
the assembly code.
4. Text Editor: Any text editor (e.g., Notepad++, Visual Studio Code) for
writing the assembly code.
5. Operating System: A compatible operating system (Linux or Windows
with WSL).
Theory
Assembly language is a low-level programming language that provides a
way to write programs that can directly manipulate hardware. In this lab, we
will implement a program that counts the number of positive and negative
integers in a predefined array. The program will iterate through the array,
check each element, and maintain counters for positive and negative
numbers.
Key Concepts:
Array: A collection of elements stored in contiguous memory locations.
Looping: A control structure that allows repeated execution of a block
of code.
Conditionals: Statements that execute different code based on certain
conditions.
Procedure
1. Setup the Environment: Install NASM and a suitable text editor.
2. Write the Assembly Code: Create a new file with a .asm extension and
write the assembly code.
3. Assemble the Code: Use NASM to assemble the code into an object
file.
4. Link the Object File: Link the object file to create an executable.
5. Run the Program: Execute the program and observe the output.
6. Record the Results: Note the counts of positive and negative numbers.
Assembly Code
section .data
student_name db "Student Name: Your Name", 0 ; Replace "Your Name
array db 5, -3, 8, -1, 0, 7, -2, 4, -5, 6 ; Sample array of signe
array_length equ 10 ; Length of the array
positive_msg db "Count of Positive Numbers: ", 0
negative_msg db "Count of Negative Numbers: ", 0
result_positive db 0
result_negative db 0
section .bss
positive_count resb 1
negative_count resb 1
section .text
global _start
_start:
; Print student name
mov rax, 1 ; syscall: write
mov rdi, 1 ; file descriptor: stdout
lea rsi, [student_name]
mov rdx, 30 ; length of student_name
syscall
; Initialize counters
mov byte [positive_count], 0
mov byte [negative_count], 0
count_loop:
mov al, [rsi] ; Load the current element into al
cmp al, 0 ; Compare with 0
jg increment_positive ; If greater than 0, increment positive cou
jl increment_negative ; If less than 0, increment negative count
jmp next_element ; If equal to 0, go to the next element
increment_positive:
inc byte [positive_count] ; Increment positive count
jmp next_element
increment_negative:
inc byte [negative_count] ; Increment negative count
next_element:
inc rsi ; Move to the next element in the array
loop count_loop ; Decrement rcx and loop if not zero
; Print results
; Print positive count
mov rax, 1 ; syscall: write
mov rdi, 1 ; file descriptor: stdout
lea rsi, [positive_msg]
mov rdx, 30 ; length of positive_msg
syscall
exit:
mov rax, 60 ; syscall: exit
xor rdi, rdi ; exit code 0
syscall
print_number:
; Function to convert a number in AL to string and print it
; Implementation goes here
ret
Observations
Result
The assembly language program effectively counted the number of positive
and negative integers in the predefined array. The output displayed the
following results:
Conclusion
This lab demonstrated the ability to write an X86/64 assembly language
program that counts positive and negative numbers in an array. The use of
loops and conditional statements allowed for efficient processing of the
array elements. The program successfully met its objectives, showcasing the
power of assembly language in performing low-level data manipulation
tasks. The implementation of the counting logic was straightforward, and
the results were accurate, confirming the program's functionality. This
exercise reinforced the understanding of basic programming constructs in
assembly language, such as data handling, control flow, and output
formatting.