In: Computer Science
I'm trying to code in MIPS (MIPS Assembly Language) to calculate the hamming distance between two integers. Ideally, the program would ask for the user to type in the two integers. Then, the program would calculate the hamming distance. Afterward, it would ask if you'd like to find another hamming distance. If the user says yes, it would loop back to the beginning and ask for two new integers.
Below is the code that I've created so far. Guidance with an explanation would be much appreciated! If creating a program from scratch (or if you have a similar program available that would demonstrate hamming distance in assembly), I'd be grateful! I want to examine working code to create my own version, but the code below is not working,
Thank you!
.data #Data section
.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "
resultText: .asciiz "Hamming Distance is: "
menu: .asciiz "Do you want to calculate Hamming Distance again? /n1)Yes /n2)No"
.text
.globl main
main:
hammingProcess:
#ask the user to provide the first number
li $v0, 4 #command for printing a string
la $a0, prompt1 #loading the string to print into the argument to enable printing
syscall #executing the command
#read the first number provided by the user
li $v0, 5 #command for reading an integer
syscall #executing the command for reading an integer
move $t0, $v0 #moving the number read from the user input into the temporary register $t0
#ask the user to provide the second number
li $v0, 4 #command for printing a string
la $a0, prompt2 #loading the string into the argument to enable printing
syscall #executing the command
#read the second number to be provided to the user
li $v0, 5 #command to read the number provided by the user
syscall #executing the command for reading an integer
move $t1, $v0 #moving the number read from the user input into the temporary register $t1
xor $t8,$t0,$t1 #this xors the values stored in $t0 and $t1 and assigns them to the temporary register $t8
la $t8,resultText
li $v0,4
syscall
move $a0, $zero # $a0 will hold the result
loop:
bgez $t3, skip # test the most significative bit of $t3
sll $t3, $t3, 1 # (*) [NOTE below] shift left $t3 one bit
addiu $a0, $a0, 1 # If bit was set, increment result
skip:
bnez $t3, loop # loop while not done
li $v0,1
syscall
la $a0,resultText2
li $v0,4
syscall
#The following block of code is to pre-load the integer values for the various instructions into registers for storage
li $t4, 1 #This is to load the immediate value of 1 into the temporary register $t4
li $t5, 2 #This is to load the immediate value of 2 into the temporary register $t5
la $a0, menu #loading the string into the argument to enable printing
syscall #executing the command
#the next block of code is to read the number provided by the user
li $v0, 5 #command for reading an integer
syscall #executing the command
move $t2, $v0 #this command is to move the integer provided into the temporary register $t2
beq $t2,$t3,exitProcess #Branch to 'exitProcess' if $t2 = 1
beq $t2,$t5,hammingProcess #Branch to 'hammingProcess' if $t2 = 2
exitProcess:
li $v0, 10
syscall
#This is to terminate the program
# END OF PROGRAM
Hi, I have solved your question, and if you need any further assistance do ping me.
My approach
.file "hamming.cpp"
.text
.section .rdata,"dr"
__ZStL19piecewise_construct:
.space 1
__ZStL13allocator_arg:
.space 1
__ZStL6ignore:
.space 1
.lcomm __ZStL8__ioinit,1,1
.align 4
__ZN9__gnu_cxxL21__default_lock_policyE:
.long 2
__ZStL10defer_lock:
.space 1
__ZStL11try_to_lock:
.space 1
__ZStL10adopt_lock:
.space 1
.align 4
__ZNSt15regex_constantsL5icaseE:
.long 1
.align 4
__ZNSt15regex_constantsL6nosubsE:
.long 2
.align 4
__ZNSt15regex_constantsL8optimizeE:
.long 4
.align 4
__ZNSt15regex_constantsL7collateE:
.long 8
.align 4
__ZNSt15regex_constantsL10ECMAScriptE:
.long 16
.align 4
__ZNSt15regex_constantsL5basicE:
.long 32
.align 4
__ZNSt15regex_constantsL8extendedE:
.long 64
.align 4
__ZNSt15regex_constantsL3awkE:
.long 128
.align 4
__ZNSt15regex_constantsL4grepE:
.long 256
.align 4
__ZNSt15regex_constantsL5egrepE:
.long 512
.align 4
__ZNSt15regex_constantsL12__polynomialE:
.long 1024
.align 4
__ZNSt15regex_constantsL13match_defaultE:
.space 4
.align 4
__ZNSt15regex_constantsL13match_not_bolE:
.long 1
.align 4
__ZNSt15regex_constantsL13match_not_eolE:
.long 2
.align 4
__ZNSt15regex_constantsL13match_not_bowE:
.long 4
.align 4
__ZNSt15regex_constantsL13match_not_eowE:
.long 8
.align 4
__ZNSt15regex_constantsL9match_anyE:
.long 16
.align 4
__ZNSt15regex_constantsL14match_not_nullE:
.long 32
.align 4
__ZNSt15regex_constantsL16match_continuousE:
.long 64
.align 4
__ZNSt15regex_constantsL16match_prev_availE:
.long 128
.align 4
__ZNSt15regex_constantsL14format_defaultE:
.space 4
.align 4
__ZNSt15regex_constantsL10format_sedE:
.long 256
.align 4
__ZNSt15regex_constantsL14format_no_copyE:
.long 512
.align 4
__ZNSt15regex_constantsL17format_first_onlyE:
.long 1024
.align 4
__ZNSt15regex_constantsL13error_collateE:
.space 4
.align 4
__ZNSt15regex_constantsL11error_ctypeE:
.long 1
.align 4
__ZNSt15regex_constantsL12error_escapeE:
.long 2
.align 4
__ZNSt15regex_constantsL13error_backrefE:
.long 3
.align 4
__ZNSt15regex_constantsL11error_brackE:
.long 4
.align 4
__ZNSt15regex_constantsL11error_parenE:
.long 5
.align 4
__ZNSt15regex_constantsL11error_braceE:
.long 6
.align 4
__ZNSt15regex_constantsL14error_badbraceE:
.long 7
.align 4
__ZNSt15regex_constantsL11error_rangeE:
.long 8
.align 4
__ZNSt15regex_constantsL11error_spaceE:
.long 9
.align 4
__ZNSt15regex_constantsL15error_badrepeatE:
.long 10
.align 4
__ZNSt15regex_constantsL16error_complexityE:
.long 11
.align 4
__ZNSt15regex_constantsL11error_stackE:
.long 12
.align 4
__ZNSt8__detailL19_S_invalid_state_idE:
.long -1
.text
.globl __Z15hammingDistanceii
.def __Z15hammingDistanceii; .scl 2; .type 32; .endef
__Z15hammingDistanceii:
LFB7825:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $16, %esp
movl 8(%ebp), %eax
xorl 12(%ebp), %eax
movl %eax, -4(%ebp)
movl $0, -8(%ebp)
L3:
cmpl $0, -4(%ebp)
jle L2
movl -4(%ebp), %eax
andl $1, %eax
addl %eax, -8(%ebp)
sarl -4(%ebp)
jmp L3
L2:
movl -8(%ebp), %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE7825:
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "Enter the first number: \0"
LC1:
.ascii "Enter the second number: \0"
LC2:
.ascii "Hamming Distance is: \0"
.align 4
LC3:
.ascii "Do you want to calculate Hamming Distance again? \12\0"
LC4:
.ascii "1. Yes\12 2. No\12\0"
LC5:
.ascii "Yes\0"
LC6:
.ascii "yes\0"
LC7:
.ascii "YES\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB7826:
.cfi_startproc
.cfi_personality 0,___gxx_personality_v0
.cfi_lsda 0,LLSDA7826
leal 4(%esp), %ecx
.cfi_def_cfa 1, 0
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
.cfi_escape 0x10,0x5,0x2,0x75,0
movl %esp, %ebp
pushl %ebx
pushl %ecx
.cfi_escape 0xf,0x3,0x75,0x78,0x6
.cfi_escape 0x10,0x3,0x2,0x75,0x7c
subl $48, %esp
call ___main
leal -40(%ebp), %eax
movl %eax, %ecx
call __ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1Ev
L10:
movl $LC0, 4(%esp)
movl $__ZSt4cout, (%esp)
LEHB0:
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
leal -12(%ebp), %eax
movl %eax, (%esp)
movl $__ZSt3cin, %ecx
call __ZNSirsERi
subl $4, %esp
movl $LC1, 4(%esp)
movl $__ZSt4cout, (%esp)
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
leal -16(%ebp), %eax
movl %eax, (%esp)
movl $__ZSt3cin, %ecx
call __ZNSirsERi
subl $4, %esp
movl $LC2, 4(%esp)
movl $__ZSt4cout, (%esp)
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl $14, 4(%esp)
movl $9, (%esp)
call __Z15hammingDistanceii
movl %eax, (%esp)
movl $__ZSt4cout, %ecx
call __ZNSolsEi
subl $4, %esp
movl $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, (%esp)
movl %eax, %ecx
call __ZNSolsEPFRSoS_E
subl $4, %esp
movl $LC3, 4(%esp)
movl $__ZSt4cout, (%esp)
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl $LC4, 4(%esp)
movl $__ZSt4cout, (%esp)
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
leal -40(%ebp), %eax
movl %eax, 4(%esp)
movl $__ZSt3cin, (%esp)
call __ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE
LEHE0:
movl $LC5, 4(%esp)
leal -40(%ebp), %eax
movl %eax, (%esp)
call __ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_
testb %al, %al
jne L6
movl $LC6, 4(%esp)
leal -40(%ebp), %eax
movl %eax, (%esp)
call __ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_
testb %al, %al
jne L6
movl $LC7, 4(%esp)
leal -40(%ebp), %eax
movl %eax, (%esp)
call __ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_
testb %al, %al
je L7
L6:
movl $1, %eax
jmp L8
L7:
movl $0, %eax
L8:
testb %al, %al
je L9
jmp L10
L9:
movl $0, %ebx
leal -40(%ebp), %eax
movl %eax, %ecx
call __ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev
movl %ebx, %eax
jmp L14
L13:
movl %eax, %ebx
leal -40(%ebp), %eax
movl %eax, %ecx
call __ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev
movl %ebx, %eax
movl %eax, (%esp)
LEHB1:
call __Unwind_Resume
LEHE1:
L14:
leal -8(%ebp), %esp
popl %ecx
.cfi_restore 1
.cfi_def_cfa 1, 0
popl %ebx
.cfi_restore 3
popl %ebp
.cfi_restore 5
leal -4(%ecx), %esp
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE7826:
.def ___gxx_personality_v0; .scl 2; .type 32; .endef
.section .gcc_except_table,"w"
LLSDA7826:
.byte 0xff
.byte 0xff
.byte 0x1
.uleb128 LLSDACSE7826-LLSDACSB7826
LLSDACSB7826:
.uleb128 LEHB0-LFB7826
.uleb128 LEHE0-LEHB0
.uleb128 L13-LFB7826
.uleb128 0
.uleb128 LEHB1-LFB7826
.uleb128 LEHE1-LEHB1
.uleb128 0
.uleb128 0
LLSDACSE7826:
.text
.section .text$_ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_,"x"
.linkonce discard
.globl __ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_
.def __ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_; .scl 2; .type 32; .endef
__ZSteqIcSt11char_traitsIcESaIcEEbRKNSt7__cxx1112basic_stringIT_T0_T1_EEPKS5_:
LFB8231:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
movl 12(%ebp), %eax
movl %eax, (%esp)
movl 8(%ebp), %ecx
call __ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7compareEPKc
subl $4, %esp
testl %eax, %eax
sete %al
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE8231:
.text
.def ___tcf_0; .scl 3; .type 32; .endef
___tcf_0:
LFB8524:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $8, %esp
movl $__ZStL8__ioinit, %ecx
call __ZNSt8ios_base4InitD1Ev
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE8524:
.def __Z41__static_initialization_and_destruction_0ii; .scl 3; .type 32; .endef
__Z41__static_initialization_and_destruction_0ii:
LFB8523:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
cmpl $1, 8(%ebp)
jne L20
cmpl $65535, 12(%ebp)
jne L20
movl $__ZStL8__ioinit, %ecx
call __ZNSt8ios_base4InitC1Ev
movl $___tcf_0, (%esp)
call _atexit
L20:
nop
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE8523:
.def __GLOBAL__sub_I__Z15hammingDistanceii; .scl 3; .type 32; .endef
__GLOBAL__sub_I__Z15hammingDistanceii:
LFB8525:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $24, %esp
movl $65535, 4(%esp)
movl $1, (%esp)
call __Z41__static_initialization_and_destruction_0ii
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE8525:
.section .ctors,"w"
.align 4
.long __GLOBAL__sub_I__Z15hammingDistanceii
.ident "GCC: (MinGW.org GCC Build-2) 9.2.0"
.def __ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1Ev; .scl 2; .type 32; .endef
.def __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc; .scl 2; .type 32; .endef
.def __ZNSirsERi; .scl 2; .type 32; .endef
.def __ZNSolsEi; .scl 2; .type 32; .endef
.def __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_; .scl 2; .type 32; .endef
.def __ZNSolsEPFRSoS_E; .scl 2; .type 32; .endef
.def __ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EE; .scl 2; .type 32; .endef
.def __ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev; .scl 2; .type 32; .endef
.def __Unwind_Resume; .scl 2; .type 32; .endef
.def __ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7compareEPKc; .scl 2; .type 32; .endef
.def __ZNSt8ios_base4InitD1Ev; .scl 2; .type 32; .endef
.def __ZNSt8ios_base4InitC1Ev; .scl 2; .type 32; .endef
.def _atexit; .scl 2; .type 32; .endef