Assembly Language Coding Problems using the alphabet? Please help. Thanks. Assem
ID: 3920022 • Letter: A
Question
Assembly Language Coding Problems using the alphabet? Please help. Thanks.
Assemble, link, and run the "printAlpha.s" (shown at bottom) assembly language program using the following steps:
1) as printAlpha.s -o printAlpha.o
2) gcc printAlpha.o
3) ./a.out
The program prints the alphabetical characters from 'a' to 'z' on one line as follows:
Make 2 copies of the printAlpha.s file, and call them mountainTerrain.s, and triangle.s
PROBLEMS:
Problem 1: Edit the mountainTerrain.s file and change the code to alternature between upper and lower case characters while printing the alphabet. The output should be as follows:
Problem 2: Edit the triangle.s file and change the code to print a triangle using lower case characters. The output should be as follows:
printAlpha.s file in text format:
.intel_syntax noprefix
.data
n1:
.int 97
n2:
.int 122
.text
.globl main
main: mov ebx, DWORD PTR n1
loop: cmp ebx, DWORD PTR n2
jg end
mov edi, ebx
call putchar
inc ebx
jmp loop
end: mov edi, 10
call putchar
mov eax, 0
ret
abcdefghijklmnopqrstuvwxyz
$ .intel syntax noprefix .data 97 4 5 n2: 6 7 8 .int .int 122 .text .globl main 11 main:mov 12 loopmp 13 mov call inc mp mov call mov ret ebx, DWORD PTR nl ebx, DWORD PTR n2 end edi, ebx putchar ebx loop edi, 10 putchar eax, 0 17 18 end: 19 20 21
Explanation / Answer
Problem 1)
Solution 1: Use conditional branch to output alternatively upper and lower case letters.
.intel_syntax noprefix
.data
n1:
.int 97
n2:
.int 122
.text
.globl main
main:
mov ebx, DWORD PTR n1
loop:
cmp ebx, DWORD PTR n2
jg end
cmp ebx, 97
jge ucase
add ebx, 32
mov edi, ebx
call putchar
inc ebx
jmp loop
ucase:
sub ebx, 32
mov edi, ebx
call putchar
inc ebx
jmp loop
end: mov edi, 10
call putchar
mov eax, 0
ret
Solution 2: You can use another variable as flag whose value will toggle between 0 or 1. Consider when this flag is 0 then print uppercase letter or if 1 then print lowercase letters. Initialize the flag as 0.
.intel_syntax noprefix
.data
n1:
.int 97
n2:
.int 122
.text
.globl main
main:
mov ebx, DWORD PTR n1
mov ecx, 0
loop:
cmp ebx, DWORD PTR n2
jg end
cmp ecx, 0
je ucase
add ebx, 32
mov edi, ebx
call putchar
inc ebx
mov ecx, 0
jmp loop
ucase:
sub ebx, 32
mov edi, ebx
call putchar
inc ebx
mov ecx, 1
jmp loop
end: mov edi, 10
call putchar
mov eax, 0
ret
Problem 2) Create two loops, outer loop will execute 26 time to print the series and inner loop will execute edx-ecx times so as ecx increases number of times inner loop executes decreases.
.intel_syntax noprefix
.data
n1:
.int 97
n2:
.int 122
.text
.globl main
main: mov ecx, 0
mov edx, 26
LOOP:
cmp ecx, 26
bge end
mov ebx, DWORD PTR n1
sub edx, edx, ecx
LOOP2:
cmp edx, 0
jle INCR
mov edi, ebx
call putchar
inc ebx
dec edx
jmp LOOP2
INCR:
inc ecx
jmp LOOP
end: mov edi, 10
call putchar
mov eax, 0
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.