In this particular use case we are trying to determine if a programmer who doesn’t know Assembly language could use ChatGPT to maintain an application. Here are examples based on some Assembly code for reversing a string.
Here is a README file ChatGPT can generate for the code.
This Assembly program reverses a given string and prints the reversed string on the screen.
The provided Assembly code consists of the following segments:
The main procedure (MAIN PROC FAR
) is responsible for:
The reverse function (REVERSE PROC
) is responsible for:
To use this Assembly code, follow these steps:
Generate a README for this Assembly Code. Show the final results as markdown code so I cut and paste it into a document called assembly.md.
---Assembly Code---
.MODEL SMALL
.STACK 100H
.DATA
; The string to be printed
STRING DB 'This is a sample string', '$'
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
; call reverse function
CALL REVERSE
; load address of the string
LEA DX,STRING
; output the string
; loaded in dx
MOV AH, 09H
INT 21H
; interrupt to exit
MOV AH, 4CH
INT 21H
MAIN ENDP
REVERSE PROC
; load the offset of
; the string
MOV SI, OFFSET STRING
; count of characters of the;
;string
MOV CX, 0H
LOOP1:
; compare if this is;
;the last character
MOV AX, [SI]
CMP AL, '$'
JE LABEL1
; else push it in the;
;stack
PUSH [SI]
; increment the pointer;
;and count
INC SI
INC CX
JMP LOOP1
LABEL1:
; again load the starting;
;address of the string
MOV SI, OFFSET STRING
LOOP2:
;if count not equal to zero
CMP CX,0
JE EXIT
; pop the top of stack
POP DX
; make dh, 0
XOR DH, DH
; put the character of the;
;reversed string
MOV [SI], DX
; increment si and;
;decrement count
INC SI
DEC CX
JMP LOOP2
EXIT:
; add $ to the end of string
MOV [SI],'$ '
RET
REVERSE ENDP
END MAIN
---End Assembly Code---
Here is an example of adding two new functions to some Assembly code and testing it via a generated output.
Here is the modified Assembly code to count and display the characters in the string and count and display the vowels in the string:
.MODEL SMALL
.STACK 100H
.DATA
; The string to be printed
STRING DB 'It is a beautiful day in Savannah, GA! You should visit sometime', '$'
VOWELS DB 'AEIOUaeiou'
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
; call reverse function
CALL REVERSE
; load address of the string
LEA DX,STRING
; output the string
; loaded in dx
MOV AH, 09H
INT 21H
; call count characters function
CALL COUNT_CHARACTERS
; call count vowels function
CALL COUNT_VOWELS
; interrupt to exit
MOV AH, 4CH
INT 21H
MAIN ENDP
REVERSE PROC
;... (same as before)
REVERSE ENDP
COUNT_CHARACTERS PROC
LEA SI, STRING
MOV CX, 0
COUNT_LOOP:
LODSB
CMP AL, '$'
JE DISPLAY_CHARACTERS
INC CX
JMP COUNT_LOOP
DISPLAY_CHARACTERS:
MOV AX, 2
MOV DX, CX
INT 2FH
RET
COUNT_CHARACTERS ENDP
COUNT_VOWELS PROC
LEA SI, STRING
MOV CX, 0
MOV DX, 0
VOWELS_LOOP:
LODSB
CMP AL, '$'
JE DISPLAY_VOWELS
PUSH AX
LEA DI, VOWELS
VOWELS_CHECK_LOOP:
LODSB
CMP AL, 0
JE NEXT_CHARACTER
CMP [SP], AL
JNE VOWELS_CHECK_LOOP
INC CX
JMP NEXT_CHARACTER
NEXT_CHARACTER:
POP AX
JMP VOWELS_LOOP
DISPLAY_VOWELS:
MOV AX, 2
MOV DX, CX
INT 2FH
RET
COUNT_VOWELS ENDP
END MAIN
For the given string “It is a beautiful day in Savannah, GA! You should visit sometime”, the output will display the reversed string, the character count (62), and the vowel count (25).
Modify this Assembly Code. Add a function to count and display the characters in the string and another function to count display the vowels in the string. Then show what the output would be for the string, "It is a beautiful day in Savannah, GA! You should visit sometime"
Show the final results as github markdown code formatted for assembly so I cut and paste it into a document called assembly.md.
---Assembly Code---
.MODEL SMALL
.STACK 100H
.DATA
; The string to be printed
STRING DB 'This is a sample string', '$'
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
; call reverse function
CALL REVERSE
; load address of the string
LEA DX,STRING
; output the string
; loaded in dx
MOV AH, 09H
INT 21H
; interrupt to exit
MOV AH, 4CH
INT 21H
MAIN ENDP
REVERSE PROC
; load the offset of
; the string
MOV SI, OFFSET STRING
; count of characters of the;
;string
MOV CX, 0H
LOOP1:
; compare if this is;
;the last character
MOV AX, [SI]
CMP AL, '$'
JE LABEL1
; else push it in the;
;stack
PUSH [SI]
; increment the pointer;
;and count
INC SI
INC CX
JMP LOOP1
LABEL1:
; again load the starting;
;address of the string
MOV SI, OFFSET STRING
LOOP2:
;if count not equal to zero
CMP CX,0
JE EXIT
; pop the top of stack
POP DX
; make dh, 0
XOR DH, DH
; put the character of the;
;reversed string
MOV [SI], DX
; increment si and;
;decrement count
INC SI
DEC CX
JMP LOOP2
EXIT:
; add $ to the end of string
MOV [SI],'$ '
RET
REVERSE ENDP
END MAIN
---End Assembly Code---
In this example we translated the code we just created into Java.
public class AssemblyToJava {
public static void main(String[] args) {
String inputString = "It is a beautiful day in Savannah, GA! You should visit sometime";
String reversedString = reverse(inputString);
System.out.println(reversedString);
int characterCount = countCharacters(inputString);
System.out.println("Character count: " + characterCount);
int vowelCount = countVowels(inputString);
System.out.println("Vowel count: " + vowelCount);
}
public static String reverse(String inputString) {
StringBuilder reversedString = new StringBuilder(inputString);
return reversedString.reverse().toString();
}
public static int countCharacters(String inputString) {
return inputString.length();
}
public static int countVowels(String inputString) {
String vowels = "AEIOUaeiou";
int vowelCount = 0;
for (int i = 0; i < inputString.length(); i++) {
char currentChar = inputString.charAt(i);
if (vowels.indexOf(currentChar) != -1) {
vowelCount++;
}
}
return vowelCount;
}
}
Translate this Assembly code into Java:
.MODEL SMALL
.STACK 100H
.DATA
; The string to be printed
STRING DB 'It is a beautiful day in Savannah, GA! You should visit sometime', '$'
VOWELS DB 'AEIOUaeiou'
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
; call reverse function
CALL REVERSE
; load address of the string
LEA DX,STRING
; output the string
; loaded in dx
MOV AH, 09H
INT 21H
; call count characters function
CALL COUNT_CHARACTERS
; call count vowels function
CALL COUNT_VOWELS
; interrupt to exit
MOV AH, 4CH
INT 21H
MAIN ENDP
REVERSE PROC
;... (same as before)
REVERSE ENDP
COUNT_CHARACTERS PROC
LEA SI, STRING
MOV CX, 0
COUNT_LOOP:
LODSB
CMP AL, '$'
JE DISPLAY_CHARACTERS
INC CX
JMP COUNT_LOOP
DISPLAY_CHARACTERS:
MOV AX, 2
MOV DX, CX
INT 2FH
RET
COUNT_CHARACTERS ENDP
COUNT_VOWELS PROC
LEA SI, STRING
MOV CX, 0
MOV DX, 0
VOWELS_LOOP:
LODSB
CMP AL, '$'
JE DISPLAY_VOWELS
PUSH AX
LEA DI, VOWELS
VOWELS_CHECK_LOOP:
LODSB
CMP AL, 0
JE NEXT_CHARACTER
CMP [SP], AL
JNE VOWELS_CHECK_LOOP
INC CX
JMP NEXT_CHARACTER
NEXT_CHARACTER:
POP AX
JMP VOWELS_LOOP
DISPLAY_VOWELS:
MOV AX, 2
MOV DX, CX
INT 2FH
RET
COUNT_VOWELS ENDP
END MAIN