Assembly Language Problem Help: I already finished problem 1 but I really need h
ID: 3919990 • Letter: A
Question
Assembly Language Problem Help:
I already finished problem 1 but I really need help with problems 2 & 3:
printAlpha.s:
.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
OR, here is a picture of printAlpha.s:
.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
Explanation / Answer
Problem 1)
.intel_syntax noprefix
.data
n1: .int 97 # initialize n1=97
n2: .int 122 # initialize n2=122
.text
.globl main
main: mov ebx, DWORD PTR n1 # move value of n1 into register ebx
loop: cmp ebx, DWORD PTR n2 # check if ebx>n2
jg end # if yes then jump to end
mov edi, ebx # if no then move ebx into edi
call putchar # call C lib func putchar to print value in edi copied above
inc ebx # increment ebx by 1
jmp loop # loop again
end: mov edi, 10 # move 10 into edi
call putchar #call C lib func
mov eax, 0 # reset eax to 0
ret # return from main
Problem 2)
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 3) 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.