180 likes | 257 Views
Assembly Language. Logical Instructions. and destination, source and AL, 01H or destination, source or AX, FAH xor destination, source xor EAX, DDFFH not destination not EBX. Logical Instructions. and AL,01H je bit_is_zero <code to be executed when the bit is one>
E N D
Logical Instructions and destination, source and AL, 01H or destination, source or AX, FAH xor destination, source xor EAX, DDFFH not destination not EBX
Logical Instructions and AL,01H je bit_is_zero <code to be executed when the bit is one> jmp skip1 bit_is_zero: <code to be executed when the bit is zero> skip1: <rest of the code>
Shift Instructions shl (Shif Left) and shr (Shift Right). These instruction is used to left/right-shift a 8,16, or 32 bit destination operand stored either in a register or in memory. • shl (Shif Left) [shl destination, count] shifts to the left by one bit position causes the leftmost bit to move to the carry flag (CF). • shl (Shif Left) [shr destination, count] shifts to the right by one bit position causes the rightmost bit to move to the carry flag (CF).
Shift Instructions Examples
Shift Instructions Testing the least significant bit of the data in the AL register. . . . shr AL,1 jnc bit_is_zero <Executed when the bit is one> jmp skip1 bit_is_zero: <Executed when the bit is zero> skip1: <rest of the code>
Rotate Instructions • Rotate without carry The rol instruction [rol destination, count] performs left rotation with the bits falling off on the left placed on the right side • The ror instruction [ror destination, count] performs right rotation
Rotate Instructions Examples
Rotate Instructions • Rotate Through Carry The rcl (Rotate through Carry Left) and rcr (Rotate through Carry Right) instruction include the carry flag in the rotation process. • rcl AL, 1 ; [rcl destination, count] • rcr BL, 1 ; [rcl destination, count]
Rotate Instructions Examples
Defining Constants The EQU Directive Format: name EQU expression Assigns the result of the expression to name. Example: NUM_OF_STUDENTS EQU 90 Assigns 90 to NUM_OF_STUDENTS … mov ECX,NUM_OF_STUDENTS cmp EAX,NUM_OF_STUDENTS Example: NUM_OF_ROWS EQU 50 NUM_OF_COLS EQU 10 ARRAY_SIZE EQU NUM_OF_ROWS * NUM_OF_COLS
Macros • A mean to represent a block (code or data) using a name, called macro name Format: %macromacro_name para_count <macro body> %endmacro Example: %macro doubleAX shl AX,1 %endmacro Using a Macro: mov AX, 23 doubleAX
Macros Macro with Parameters Example: %macro mult_by_16 1 shl %1, 4 %endmacro Using a Macro: mov BX, 15 mult_by_16 BX
Illustration Example ; Convert an input the asky of an input key into binary .DATA prompt db "Please input a character: ",0 ascii_msg db "The ASCII code of ’",0 binary_msg db "’ in binary is ",0 query_msg db "Do you want to quit (Y/N): ",0 .CODE .STARTUP read_char: PutStr prompt ; request a char. input GetCh AL ; read input character PutStr ascii_msg PutCh AL PutStr binary_msg mov AH,80H ; mask byte = 80H mov ECX,8 ; loop count to print 8 bits
Illustration Example print_bit: test AL,AH ; test does not modify AL jz print_0 ; if tested bit is 0, print it PutCh ’1’ ; otherwise, print 1 jmp skip1 print_0: PutCh ’0’ ; print 0 skip1: shr AH,1 ; right-shift mask bit to test loop print_bit ; next bit of the ASCII code newline PutStr query_msg ; query user whether to terminate GetCh AL ; read response cmp AL,’Y’ ; if response is not ’Y’ jne read_char ; read another character done: ; terminate program .EXIT
Illustration Example %include "io.mac" .DATA char_prompt db "Please input a character: ",0 out_msg1 db "The ASCII code of ’",0 out_msg2 db "’ in hex is ",0 query_msg db "Do you want to quit (Y/N): ",0 .CODE .STARTUP read_char: PutStr char_prompt ; request a char. input GetCh AL ; read input character PutStr out_msg1 PutCh AL PutStr out_msg2 mov AH,AL ; save input character in AH shr AL,4 ; move upper 4 bits to lower half mov CX,2 ; loop count - 2 hex digits to print
Illustration Example print_digit: cmp AL,9 ; if greater than 9 jg A_to_F ; convert to A through F digits add AL,’0’ ; otherwise, convert to 0 through 9 jmp skip A_to_F: add AL,’A’-10 ; subtract 10 and add ’A’ to convert to A through F skip: PutCh AL ; write the first hex digit mov AL,AH ; restore input character in AL and AL,0FH ; mask off the upper half-byte loop print_digit newline ; move to next line (macro) PutStr query_msg ; query user whether to terminate GetCh AL ; read response cmp AL,’Y’ ; if response is not ’Y’ jne read_char ; read another character done: ; otherwise, terminate program .EXIT