Read More
Procedures in assembly language
INCLUDE Irvine32.inc
.data
prompt db "Please enter a hexadecimal number up to 8 digits: ",0
frequencies dd 0, 0, 0, 0, 0, 0
result1 db "Frequency table: ",10,13,0
result2 db "2^",0
result3 db "= ",0
.code
main PROC
mov edx, OFFSET prompt ; load address of string to print
call WriteString ; print the string on the screen
call ReadHex ; Read a hexadecimal number from the keyboard
mov esi, OFFSET frequencies ; point to start of frequency table
call getFrequencies ; fill in the frequencies
mov esi, OFFSET frequencies ; point to start of frequency table
call printFrequencies ; print frequency table on screen
exit ; exit the program
main ENDP
; Get the frequencies for the number given in eax
; saves them in the table pointer by esi
getFrequencies PROC
; First get all bits set:
mov ebx, 1 ; start in bit 1
testLoop:
cmpeax, 0 ; see if number is zero
je endLoop ; if zero, end loop
test eax, 1 ; if current bit is set
jz nextBit ; if not, go to next bit
; if set, update frequencies
mov edx, ebx ; get current bit position in edx
mov ecx, 0 ; current power being tested
updateLoop:
test edx, 1 ; check if this power of 2 is active
jznextPow ; if not, test next power
inc DWORD PTR[esi + ecx*4] ; increment frequency of current power of 2
nextPow:
shr edx, 1 ; test following power of 2 by shifting
incecx ; increment power position in table
cmpecx, 5 ; see if we compared all powers
jle updateLoop ; repeat if power <= 5
nextBit:
inc ebx ; increment bit number
shreax, 1 ; move next bit to the right
jmp testLoop ; repeat loop
endLoop:
ret
getFrequencies ENDP
; Print the frequency table on the screen
; the table pointer is given in esi
printFrequencies PROC
mov edx, OFFSET result1 ; load address of string to print
call WriteString ; print the string on the screen
mov ecx, 0 ; current power
printLoop:
mov edx, OFFSET result2 ; load address of string to print
call WriteString ; print the string on the screen
mov eax, ecx ; load current power
call WriteDec ; print on screen
mov edx, OFFSET result3 ; load address of string to print
call WriteString ; print the string on the screen
mov eax, [esi + ecx*4] ; load current frequency
call WriteDec ; print on screen
call CrLF ; jump to next line
inc ecx ; increment power
cmp ecx, 5 ; compare power with 5
jle printLoop ; continue printing while power<=5
ret
printFrequencies ENDP
END main
Arrays in assembly language
@7 // value 7
D=A // load value in D
@R1 // load R1 address
M=D // save 7 in R1
@11 // value 11
D=A // load in D
@R2 // load R2 address
M=D // save 11 in R2
@5 // value 5
D=A // load in D
@R3 // load R3 address
M=D // save 5 in R3
@R1 // load R1 address
D=M // load R1 value in D
@R2 // load R2 address
D=D-M // subtract R1-R2
@ELSE2 // load address of label
D;JLT // jump if R1 < R2
@R1 // load R1 address
D=M // load R1 value in D
@R3 // load R3 address
D=D-M // subtract R1-R3
@ELSE1 // load address of label
D;JLT // jump if R1 < R3
@R1 // load R1 address
D=M // load R1 value in D
@R0 // load R0 address
M=D // R0 = R1
@END // load address of end label
0; JMP // jump to end of program
(ELSE1)
@R3 // load R3 address
D=M // load R3 value in D
@R0 // load R0 address
M=D // R0 = R3
@END // load address of end label
0; JMP // jump to end of program
(ELSE2)
@R2 // load R2 address
D=M // load R2 value in D
@R3 // load R3 address
D=D-M // subtract R2-R3
@ELSE3 // load address of label
D;JLT // jump if R2 < R3
@R2 // load R2 address
D=M // load R2 value in D
@R0 // load R0 address
M=D // R0 = R2
@END // load address of end label
0; JMP // jump to end of program
(ELSE3)
@R3 // load R3 address
D=M // load R3 value in D
@R0 // load R0 address
M=D // R0 = R3
(END)
@END // load address of end
0;JMP // loop indefinitely