Difference between revisions of "CSC231 Lab 1 Solutions 2017"
(Created page with "--~~~~ ---- =Program 1= <br /> <source lang="asm"> ;;; lab1 problem 1 ;;; D. Thiebaut ;;; ;;; Prints boxed string ;;; ;;; To assemble, link, and run: ;;; ...") |
(→Program 4) |
||
Line 135: | Line 135: | ||
<br /> | <br /> | ||
<source lang="asm"> | <source lang="asm"> | ||
− | ;;; | + | ;;; lab1_4.asm |
;;; D. Thiebaut | ;;; D. Thiebaut | ||
;;; | ;;; | ||
+ | ;;; Prints a triangle of stars on 5 lines | ||
+ | ;;; | ||
;;; To assemble, link, and run: | ;;; To assemble, link, and run: | ||
− | ;;; nasm -f elf | + | ;;; nasm -f elf lab1_4.asm |
− | ;;; ld -melf_i386 -o | + | ;;; ld -melf_i386 -o lab1_4 lab1_4.o |
− | ;;; ./ | + | ;;; ./lab1_4 |
;;; | ;;; | ||
Revision as of 20:18, 1 February 2017
--D. Thiebaut (talk) 20:18, 1 February 2017 (EST)
Contents
Program 1
;;; lab1 problem 1
;;; D. Thiebaut
;;;
;;; Prints boxed string
;;;
;;; To assemble, link, and run:
;;; nasm -f elf helloWorld.asm
;;; ld -melf_i386 -o helloWorld helloWorld.o
;;; ./helloWorld
;;;
section .data
msg db "*********************************", 10
db "* Welcome to CSC231 *", 10
db "* Home of the Assembly Language *", 10
db "*********************************", 10
msgLen equ $-msg
section .text
global _start
_start:
;;; print message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg ; address of message to print
mov edx, msgLen ; # of chars to print
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
Program 2
;;; lab1 problem 2
;;; D. Thiebaut
;;;
;;; Prints boxed string, but declares only 1 line of stars
;;;
;;; To assemble, link, and run:
;;; nasm -f elf lab1_2.asm
;;; ld -melf_i386 -o lab1_2 lab1_2.o
;;; ./lab1_2
;;;
section .data
msg db "*********************************", 10
msgLen1 equ $-msg
db "* Welcome to CSC231 *", 10
db "* Home of the Assembly Language *", 10
msgLen equ $-msg
section .text
global _start
_start:
;;; print first 3 lines of message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg ; address of message to print
mov edx, msgLen ; # of chars to print
int 0x80
;;; print first line one more time, to close the box
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, msg ; address of first line
mov edx, msgLen1 ; # of chars in first line
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
Program 3
;;; helloWorld.asm
;;; D. Thiebaut
;;;
;;; Prints a triangle of stars
;;;
;;; To assemble, link, and run:
;;; nasm -f elf helloWorld.asm
;;; ld -melf_i386 -o helloWorld helloWorld.o
;;; ./helloWorld
;;;
section .data
Mesg db "*", 10
db "**", 10
db "***", 10
db "****", 10
db "*****", 10
MesgLen equ $-Mesg
section .text
global _start
_start:
;;; print message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, Mesg ; address of message to print
mov edx, MesgLen ; # of chars to print
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80
Program 4
;;; lab1_4.asm
;;; D. Thiebaut
;;;
;;; Prints a triangle of stars on 5 lines
;;;
;;; To assemble, link, and run:
;;; nasm -f elf lab1_4.asm
;;; ld -melf_i386 -o lab1_4 lab1_4.o
;;; ./lab1_4
;;;
section .data
Mesg db "*****", 10
MesgLen equ $-Mesg
section .text
global _start
_start:
;;; print message
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, Mesg+4 ; address of message to print
mov edx, 2 ; 1 * and 1 line-feed
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, Mesg+3 ; address of message to print
mov edx, 3 ; 2 * and 1 line-feed
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, Mesg+2 ; address of message to print
mov edx, 4 ; 3 * and 1 line-feed
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, Mesg+1 ; address of message to print
mov edx, 5 ; 4 * and 1 line-feed
int 0x80
mov eax, 4 ; write
mov ebx, 1 ; stdout
mov ecx, Mesg ; address of message to print
mov edx, 6 ; 5 * and 1 line-feed
int 0x80
;;; exit
mov ebx, 0
mov eax, 1
int 0x80