Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How could I change this assembly code around but still arrive at the same output

ID: 3920117 • Letter: H

Question

How could I change this assembly code around but still arrive at the same output?

The output of this is:

$ ./a.out
AbCdEfGhIjKlMnOpQrStUvWxYz
$

Text version of code:

.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

.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

.intel_syntax noprefix .data 3 nl: 4 5 n2 .int 97 .int 122 7 8 .text 9 10 11 main: mov 12 loopmp 13 14 15 .globlmain ??? dge add mov call inc mp ebx, DWORD PTR nl ebx, DWORD PTR n2 end ebx, 97 ucase ebx, 32 edi, ebx putchar ebx loop 17 18 19 20 21 22 ucase sub 23 24 25 26 27 28 end: 29 30 31 mov call inc mp ebx, 32 edi, ebx putchar ebx loop mov call mov ret edi, 10 putchar eax, 0

Explanation / Answer

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

jge 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote