板凳——————————————————(枯藤 )MIX汇编 前导(6)

本文介绍了汇编语言中字符串操作指令,包括cld、std、movsb和rep系列,以及如何使用它们进行字符串的复制、转换和比较。通过实例展示了如何处理边界条件和内存填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//2019年天津大学智算学部汇编语言程序设计-李罡
//2020年08月09日 15时05分15秒

The direction depends on DF, if DF = 1 they decrease

cld # clear the DF (= 0), increase

std # set the DF (= 0), decrease

.section .data

value1:
.ascii “This is a test string.\n”

.section .bss
.lcomm output, 23
.section .text
.globl main
main:
leal value1, %esi
leal output, %edi
movl $23, %ecx
cld

loop1: # 第一种

movsb

loop loop1

rep movsb         # 第二种

Using rep prefix to transfer string by bytes.

ECX is used to count the loop.

movl $1, %eax
movl $0, %ebx
int $0x80

#rep prefix to ransfer by blocks
# Eg. to transfer 8 bytes, when using movsb, ecx should be 8.
# If using movsw, ecx is set to 4
# Pay attention to out-of-boundary
# Eg. String length is 23 bytes. If ecx = 6, using movsl
# will transfer 24 bytes.

(gdb) x/s &output

#ox601058 : “This is a test string.\nO”

.section .data
value1:
.ascii “This is a test string.\n”
value2:
.ascii “Oops”
.section .bss
.lcomm output, 23
.section .text
.globl main
main:
leal value1, %esi
leal output, %edi
movl $6, %ecx
cld
rep movsl
movl $1, %eax
movl $0, %ebx
int $0x80

/*
wannian07@wannian07-PC:~$ gcc hello.s -o hello
wannian07@wannian07-PC:~$ ./hello
wannian07@wannian07-PC:~$

#2020年08月09日 15时31分10秒
.section .data
string1:
.ascii “This is a test of the conversion program!\n”
length:
.int 43                # length = .-output 计算长度
divisor:                  # 除法 %4
.int 4
.section .bss
.lcomm buffer, 43
.section .text
.globl main
main:
leal string1, %esi
leal buffer, %edi
movl length, %ecx       # ecx = 43  
shrl $2, %ecx # % 4 43 % 4 = 10 3
cld
rep movsl            # 做10次 rep

movl length, %ecx # ecx = 43
andl $3, %ecx # 逐位与3 (0011) 高位清零, 个位保留
rep movsb            # 做3次 rep  一共用了10 + 3 次
movl $1, %eax
movl $0, %ebx
int 0 x 80 / ∗     w a n n i a n 07 @ w a n n i a n 07 − P C :   0x80 /*   wannian07@wannian07-PC:~ 0x80/  wannian07@wannian07PC:  gcc hello.s -o hello
wannian07@wannian07-PC:~$ ./hello
wannian07@wannian07-PC:~$

#lods?
load (%esi) to AL/AX/EAX/RAX and increase / decrease esi(on DF = 0/1)
rep lodsl (in fact, rep is useless with lodsl, for there is only one eax)

#stos?
Store AL/AX/EAX/RAX to (%edi) and increase / decrease esi(on DF = 0/1)
rep stosl (fill the memory with one value)

.section .data
space:
.ascii “” # 用空格填入一片内存
.section .bss
.lcomm buffer, 256
.section .text
.globl main
main:
leal space, %esi
leal buffer, %edi
movl $256, %ecx
cld

lodsb
rep stosb
movl $1, %eax
movl $0, %ebx
int $0x80

wannian07@wannian07-PC:~$ gcc hello.s -o hello
wannian07@wannian07-PC:~$ ./hello
wannian07@wannian07-PC:~$

#fill buffer with space
#Esi, edi, eax, ecx are used

 .section .data

string1:
.ascii “This is a TEST, of the conversion program!\n”
length:
.int 43                
.section .text
.globl main
main:
leal string1, %esi
movl %esi, %edi
movl length, %ecx       
cld

loop1: #所有小写字母, 改写成大写字母
lodsb       #取第一个字符 T, 与a-z 比较. 
cmpb $‘a’, %al
jl skip      # 比较小 跳转skip
cmpb $‘z’, %al
jg skip      # 比较大,跳转skip 
subb $0x20, %al # 所有符合条件的, 减去al-20 变成大写字母

skip:
stosb
loop loop1

print:
movl $4, %eax
movl $1, %ebx
movl $string1, %ecx
movl length, %edx
int $0x80

end:
movl $1, %eax
movl $0, %ebx
int $0x80

//2020年08月09日 16时19分50秒
.section .data
value1:
.ascii “Test”
value2:
.ascii “Test”
.section .text
.globl main
main:
movl $1, %eax #比较两个字符串, 相等返回0, 不等返回 1
leal value1, %esi
leal value2, %edi
cld
cmpsl
je equal
movl $1, %ebx
int $0x80

equal:
movl $0, %ebx
int $0x80

 /*

wannian07@wannian07-PC:~$ gcc hello.s -o hello
wannian07@wannian07-PC:~$ ./hello
wannian07@wannian07-PC:~$ echo $?
0

cmps?
compare (%esi) with (%edi)
in the example, cmpsl compares 4 bytes.
meaning (%edi) - (%esi)

.section .data

value3:
.ascii “This is a test of the CMPS instructions”
value4:
.ascii “This is a test of the CMPS Instructions”  
.section .text
.globl main

main:
movl $1, %eax
leal value3, %esi
leal value4, %edi
movl $39, %ecx
cld

repe cmpsb              # 两字符比较, edi - esi
je equal            #相等ZF= 1,循环  , 不相等,退出 ;cx = 0  退出
movl %ecx, %ebx        # ecx 剩下的沒有做的數, 
int $0x80

equal:
movl $0, %ebx
int $0x80

rep with cmps
but rep only checks ecx
other than check ecx, there are reps checking zf.

   REPE     Loop if equal    Check if ZF == 0
   REPNE     Loop if not equal    Check if ZF != 0
   REPZ     Loop if zero    Same as REPE
   REPNZ     Loop if not zero    Same as REPNE




 .section .data

string1:
.ascii “test”
length1:
.int 4
string2:
.ascii “test1”
length2:
.int 5
.section .text
.globl main
main:
lea string1, %esi
lea string2, %edi
movl length1, %ecx
movl length2, %eax
cmpl %eax, %ecx
ja longer
xchg %ecx, %eax

longer:
cld
repe cmpsb
je equal
je greater

less:
movl $1, %eax
movl $225, %ebx
int $0x80

greater:
movl $1, %eax
movl $1, %ebx
int $0x80

equal:
movl length1, %ecx
movl length2, %eax
cmpl %ecx, %eax
jg greater
jl less
movl $1, %eax
movl $0, %ebx
int $0x80

wannian07@wannian07-PC:~$ gcc hello.s -o hello
wannian07@wannian07-PC:~$ ./hello
wannian07@wannian07-PC:~$ echo $?
225

test > boom
test < van
test > Test         大于 返回 1
test < test1 小于 返回225
                相等  长度一样返回 0

//http://c.biancheng.net/view/3682.html
SCASB、SCASW 和 SCASD 指令分别将 AL/AX/EAX 中的值与 EDI 寻址的一个字节 / 字 / 双字进行比较。这些指令可用于在字符串或数组中寻找一个数值。结合 REPE(或 REPZ)前缀,当 ECX > 0 且 AL/AX/EAX 的值等于内存中每个连续的值时,不断扫描字符串或数组。

REPNE 前缀也能实现扫描,直到 AL/AX/EAX 与某个内存数值相等或者 ECX = 0。
repne scanb (stop loop scan if %al equals (%edi))

扫描是否有匹配字符下面的例子扫描字符串 alpha,在其中寻找字符 F。如果发现该字符,则 EDI 指向匹配字符后面的一个位置。如果未发现匹配字符,则 JNZ 执行退出:

.data
alpha BYTE "ABCDEFGH",0
.code
mov edi,OFFSET alpha        ;ED工指向字符串
mov al, 'F'                 ;检索字符F
mov ecx,LENGTHOF alpha      ;设置检索计数器
cld                         ;方向为正向
repne seasb                 ;不相等则重复
jnz quit                    ;若未发现字符则退出
dec edi                     ;发现字符:EDI 减 1

循环之后添加了 JNZ 以测试由于 ECX=0 且没有找到 AL 中的字符而结束循环的可能性。

*/

.section .data

string1:
.ascii “This is a test - a long text string to scan.”
length:
.int 44
string2:
.ascii “-”
.section .text
.globl main

main:
leal string1, %edi
leal string2, %esi
movl length, %ecx
lodsb
cld
#REPNE 先检查CX是否为0, 不为0则循环.
repne scasb          #当CX0或者ZF1的时候终止循环.
jne notfound
subw length, %cx
neg %cx
movl $1, %eax
movl %ecx, %ebx
int $0x80

notfound:
movl $1, %eax
movl $0, %ebx
int $0x80

/*
wannian07@wannian07-PC:~$ gcc hello.s -o hello

wannian07@wannian07-PC:~$ ./hello
wannian07@wannian07-PC:~$ echo $?
16
# 在第16的位置找到了,所要求的

        涉及到三个寄存器:ECX,  AL, EDI 和标志寄存器的ZF位和DF位.


*/
<think>嗯,用户让我介绍一下如何创作一首千古绝句。首先,我需要回想之前用户问过关于古诗创作的内容,当时我详细介绍了古诗的格律、结构、押韵等要素,还给了示例。现在用户可能想要更深入的内容,特别是如何写出能流传的“千古绝句”,这需要更高的要求。 首先,我得明确千古绝句的特点。这类诗通常结构紧凑,语言凝练,意境深远,情感真挚,还要有独特的艺术手法。可能需要结合经典例子,比如李白的《静夜思》或王之涣的《登鹳雀楼》,分析它们为何成为经典。 接下来,用户可能需要具体的步骤指导。之前的回复里已经分步骤讲解了,但这次要突出“千古”这个层次,所以得强调炼字、意境、情感共鸣等方面。可能需要加入更多关于如何选择主题、运用比喻、对仗等技巧的建议。 还要注意用户可能的深层需求。他们可能不只是要写一首符合格律的诗,而是希望作品有流传的价值。所以需要强调创新性和独特性,避免陈词滥调,同时保持传统格律。可能需要提醒用户多读经典,积累词汇和意象,同时注重个人体验的表达。 另外,用户之前给的示例是五言绝句,这次可能需要扩展,比如七言或其他形式,但绝句通常是四句,所以还是保持四句的结构。需要检查平仄和押韵是否符合要求,并提供修改前后的对比,展示如何优化诗句。 最后,要确保回答结构清晰,分点说明,可能包括特点、创作方法、示例解析、常见误区等部分。使用$$...$$格式展示平仄结构,保持LaTeX语法正确。还要注意用中文口语化的思考过程,但正式回答要符合之前的格式要求,比如分步骤和示例。</think>创作千古绝句需兼具思想性、艺术性与韵律美,以下是关键要素与创作方法论: 一、千古绝句三大核心特征(以唐诗为例) 1. 时空穿透力 $$ \text{典型结构} = \text{眼前景} + \text{宇宙观} $$ 示例:王之涣《登鹳雀楼》 白日依山尽,黄河入海流 欲穷千里目,更上一层楼 2. 意象浓缩度 $$ \text{意象密度} \geq 3/\text{句} $$ 杜牧《秋夕》仅28字包含:银烛/画屏/流萤/天阶/夜色/牵牛织女星 3. 哲学纵深度 王维《鹿柴》通过"空山不见人,但闻人语响"构建"有与无"的禅意空间 二、创作四维模型 1. 炼字准则(动词选择公式) $$ \text{动词力度} = \frac{\text{动态意象}}{\text{静态场景}} $$ 贾岛"僧敲月下门"中"敲"字胜于"推",因其产生声波涟漪效应 2. 留白设计 使用蒙太奇式意象组合: ```math \begin{cases} \text{马致远《天净沙》} & : \text{枯藤+老树+昏鸦} \rightarrow \text{秋思} \\ \text{温庭筠《商山早行》} & : \text{鸡声+茅店+月} \rightarrow \text{羁旅} \end{cases} ``` 3. 音律拓扑 七绝标准平仄结构: ``` △平△仄△平△(韵) △仄平平△仄平(韵) △仄△平△仄仄 △平△仄△平△(韵) ``` (△表示可平可仄) 三、实战示范(边塞主题) 1. 原始构思: 大漠风沙暗,孤城战鼓寒 将军百战后,白骨满关山 2. 千古化改造: - 加入时空维度:羌笛→历史回响 - 植入哲学命题:生与死的辩证 - 动词升级:"暗"→"咽","满"→"生" **《凉州词》(改创版)** 羌笛幽咽云月残,孤城遥望玉门寒 黄沙埋尽将军骨,春草偏生旧箭瘢 $$ \text{平仄检测}:平平平仄平仄平,平平平仄仄平平 \\ 平平平仄平平仄,平仄平平仄仄平 $$ 四、突破性创新路径 1. 反常合道: 李贺"羲和敲日玻璃声"(N.音效=敲击太阳×玻璃声) 2. 感官通感: 李商隐"莺啼如有泪,为湿最高花"(听觉→触觉) 3. 微观史诗: 杜甫"星垂平野阔,月涌大江流"(天文×地理×流体力学) 五、检验标准 1. 可拆解性:每联独立成画(如"两个黄鹂鸣翠柳"四句即四幅工笔画) 2. 多义性:王昌龄"洛阳亲友如相问"可解读为思乡、表忠、言志三重意蕴 3. 再生性:张继《枫桥夜泊》催生寒山寺千年文化产业链 建议从"微观场景+宏观思考"入手,如写落叶可关联:"飘零非本愿,天地有轮回"。当代题材可尝试:"霓虹吞北斗,扫码问星辰"。创作后放置三日再审,检验是否经得起时间维度推敲。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值